Question

I have a nested SQL query for quiz example to update multiple tables. It contains of two case statements which check particular conditions while performing the query to DB. Here is the code:

// form data
$edit_qid = isset($_POST['editqid']);
$edit_question = isset($_POST['editquestion']);
$edit_ans1 = isset($_POST['editanswer1']);
$edit_ans2 = isset($_POST['editanswer2']);
$edit_ans3 = isset($_POST['editanswer3']);
$edit_correct = isset($_POST['editcorrect']);

// answer ids (comes from another query and it's valid)
while ($rows = mysqli_fetch_assoc($result_aid)) {
    $aid[] = $rows['aid'];
}

// update of chosen question and answer options to DB
$upd_question = "UPDATE `question_bank` qtbl INNER JOIN `answer_bank` atbl
                        SET qtbl.`question`='".$edit_question."',
                            atbl.`answer`= CASE WHEN atbl.`aid`='".$aid[0]."' THEN '".$edit_ans1."'
                                                WHEN atbl.`aid`='".$aid[1]."' THEN '".$edit_ans2."'
                                                WHEN atbl.`aid`='".$aid[2]."' THEN '".$edit_ans3."' 
                                           END
                            atbl.`correct` = CASE WHEN ".$edit_correct."=='1' THEN '1'
                                                  WHEN ".$edit_correct."=='2' THEN '1'
                                                  WHEN ".$edit_correct."=='3' THEN '1' 
                                                  ELSE '0' 
                                             END
                    WHERE qtbl.`qid`=atbl.`question_id` AND qtbl.`qid`='".$edit_qid."'";
mysqli_query($mysqli, $upd_question) or die ("<b>Update of question failed:</b> " . mysqli_error($mysqli));

The variable $edit_correct contains either value 1, 2 or 3 with respect to answer strings and according to above mentioned query to DB there will be saved 1 or 0 (true/false) meaning that if value is 1 then true will be saved for 1st answer option etc.

Running of this code gives me the following error:

Update of question failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'atbl.`correct` = CASE WHEN 2=='1' THEN '1' WHEN 2=='2' THEN '1' ' at line 7

Any ideas to resolve this issue? Thanks in advance.

UPDATE

Also, I checked with the following condition:

,atbl.`correct` = CASE WHEN '". if($edit_correct=='1') {echo 1;} ."' THEN '1'
                      WHEN '". if($edit_correct=='2') {echo 1;} ."' THEN '1'
                      WHEN '". if($edit_correct=='3') {echo 1;} ."' THEN '1' 
                      ELSE '0' 
                 END

and got this error: Parse error: syntax error, unexpected 'if' (T_IF)

No correct solution

OTHER TIPS

You have to put a comma betwen the two columns, try this code:

$upd_question = "UPDATE `question_bank` qtbl INNER JOIN `answer_bank` atbl
                        SET qtbl.`question`='".$edit_question."',
                            atbl.`answer`= CASE WHEN atbl.`aid`='".$aid[0]."' THEN '".$edit_ans1."'
                                                WHEN atbl.`aid`='".$aid[1]."' THEN '".$edit_ans2."'
                                                WHEN atbl.`aid`='".$aid[2]."' THEN '".$edit_ans3."' 
                                           END
                            ,atbl.`correct` = CASE WHEN ".$edit_correct."=='1' THEN '1'
                                                  WHEN ".$edit_correct."=='2' THEN '1'
                                                  WHEN ".$edit_correct."=='3' THEN '1' 
                                                  ELSE '0' 
                                             END
                    WHERE qtbl.`qid`=atbl.`question_id` AND qtbl.`qid`='".$edit_qid."'";
mysqli_query($mysqli, $upd_question) or die ("<b>Update of question failed:</b> " . mysqli_error($mysqli));

At the second case you can use IF() sentence, using IF will be more readable code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top