Skip to content

Fix: Quiz answer image_id empty string fails on MariaDB strict mode - #2955

Open
shsajalchowdhury wants to merge 1 commit into
themeum:devfrom
shsajalchowdhury:fix/quiz-answer-image-id-null
Open

Fix: Quiz answer image_id empty string fails on MariaDB strict mode#2955
shsajalchowdhury wants to merge 1 commit into
themeum:devfrom
shsajalchowdhury:fix/quiz-answer-image-id-null

Conversation

@shsajalchowdhury

Copy link
Copy Markdown

Summary

When saving a quiz answer without an image, the image_id value was passed as an empty string ('') to wpdb->insert(). On MariaDB strict mode (and MySQL strict mode), this causes a database insertion failure because the image_id column is defined as bigint(20) DEFAULT NULL — it only accepts integers or NULL, not empty strings.

Root Cause

  1. QuizBuilder::prepare_answer_data() calls Input::sanitize($input['image_id'] ?? null) which returns an empty string '' when no image is provided
  2. wpdb->insert() receives the empty string and passes it through wpdb->prepare('%s', '') producing '' in the SQL
  3. The image_id column (bigint(20) DEFAULT NULL) rejects '' on strict SQL modes

Changes Made

File: classes/QuizBuilder.php

  1. prepare_answer_data() (line 123) — After sanitization, convert empty image_id to null:

    $image_id = empty( $image_id ) ? null : (int) $image_id;
  2. save_question_answers() (line 177) — Filter out null values before wpdb->insert() so MySQL uses the column DEFAULT NULL:

    $insert_data = array_filter(
        $answer_data,
        function ( $value ) { return null !== $value; }
    );
    $wpdb->insert( $answers_table, $insert_data );

Steps to Reproduce

  1. Use MariaDB with strict mode (sql_mode = STRICT_TRANS_TABLES)
  2. Create a quiz with any question type that has answer options
  3. Add an answer without selecting an image
  4. Save the quiz question
  5. Before fix: Answer fails to save (database error in debug log)
  6. After fix: Answer saves correctly with image_id = NULL

Testing Instructions

  1. Switch DB to MariaDB with STRICT_TRANS_TABLES mode
  2. Create a quiz, add a question, add answers without images
  3. Verify answers are saved successfully
  4. Check the tutor_quiz_question_answers table — image_id should be NULL
  5. Also test with MySQL non-strict mode to verify no regression

Does this PR change what data or activity we track or use?

No.

Fixes #1894

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant