Fix: Quiz answer image_id empty string fails on MariaDB strict mode - #2955
Open
shsajalchowdhury wants to merge 1 commit into
Open
Fix: Quiz answer image_id empty string fails on MariaDB strict mode#2955shsajalchowdhury wants to merge 1 commit into
shsajalchowdhury wants to merge 1 commit into
Conversation
…riaDB When a quiz answer is saved without an image, the image_id field was passed as an empty string instead of NULL. On MariaDB strict mode, this causes a database error because the column expects bigint or NULL. Changes: - Convert empty image_id to null in prepare_answer_data() - Filter null values from insert data to use DB column defaults Fixes themeum#1894
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When saving a quiz answer without an image, the
image_idvalue was passed as an empty string ('') towpdb->insert(). On MariaDB strict mode (and MySQL strict mode), this causes a database insertion failure because theimage_idcolumn is defined asbigint(20) DEFAULT NULL— it only accepts integers or NULL, not empty strings.Root Cause
QuizBuilder::prepare_answer_data()callsInput::sanitize($input['image_id'] ?? null)which returns an empty string''when no image is providedwpdb->insert()receives the empty string and passes it throughwpdb->prepare('%s', '')producing''in the SQLimage_idcolumn (bigint(20) DEFAULT NULL) rejects''on strict SQL modesChanges Made
File:
classes/QuizBuilder.phpprepare_answer_data()(line 123) — After sanitization, convert emptyimage_idtonull:save_question_answers()(line 177) — Filter outnullvalues beforewpdb->insert()so MySQL uses the columnDEFAULT NULL:Steps to Reproduce
sql_mode = STRICT_TRANS_TABLES)image_id = NULLTesting Instructions
STRICT_TRANS_TABLESmodetutor_quiz_question_answerstable —image_idshould beNULLDoes this PR change what data or activity we track or use?
No.
Fixes #1894