dongxun1244 2014-02-24 11:33
浏览 60

使用PHP和MySQL中的嵌套CASE语句更新查询

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)

  • 写回答

1条回答 默认 最新

  • duanran3115 2014-02-24 11:53
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛