MySQL error with strict mode if message too long #3170
Labels
b:1.8
Branch: 1.8.x
s:resolved
Status: Resolved. Solution implemented or scheduled
t:bug
Type: Bug. An issue causing error / flaw / malfunction
Milestone
If the message is longer than the limit of the message field in the database, with MySQL strict mode enabled it throws an error instead of cutting the message after the limit.
Code:SQL Error:
1406 - Data too long for column 'message' at row 1
To avoid this error in our forum, I added in function verify_message() in inc/datahandlers/post.php below
PHP Code:// If this board has a maximum message length check if we're over it. Use strlen because SQL limits are in byteselse if(strlen($post['message']) > $mybb->settings['maxmessagelength'] && $mybb->settings['maxmessagelength'] > 0 && !is_moderator($post['fid'], "", $post['uid'])){ $this->set_error("message_too_long", array($mybb->settings['maxmessagelength'], strlen($post['message']))); return false;}
a check:
PHP Code:// If the message length is over SQL limitselse if(strlen($post['message']) > 65535){ global $db; $fieldlength = $db->show_fields_from("posts"); if(is_array($fieldlength)) { foreach ($fieldlength as $key => $fields) { if($fields['Field'] == 'message') { switch($fields['Type']) { case 'text': { $maximum = 65535; } break; case 'mediumtext': { $maximum = 16777215; } break; case 'longtext': { $maximum = 4294967295; } break; default: $maximum = 65535; } } } } if(isset($maximum) && strlen($post['message']) > $maximum) { $this->set_error("message_too_long", array($maximum, strlen($post['message']))); return false; }}
Original thread: MySQL error with strict mode if message too long
The text was updated successfully, but these errors were encountered: