大家可以给我任何提示,为什么字符串值不会传递给查询。这是代码:
要在查询中使用的数组:
Array ( [16] => 41 [17] => 0 [18] => 0 [19] => 0 [20] => 0 [21] => 0 [22] => 0 [23] => 0 [24] => 0 [25] => 0 [26] => 0 [27] => 0 [28] => 0 [29] => buu [30] => bauadaskd )
代码:
foreach ($arrAnswers as $id => $answer)
{ $update .= sprintf("WHEN '%d' THEN '%d' ", $id, $answer);
}
echo $update .= "END WHERE a_question IN ($ids) AND a_user = '{$_SESSION['current_user_id']}'";
输出
UPDATE answers SET a_answer = CASE a_question WHEN '16' THEN '41' WHEN '17' THEN '0' WHEN '18' THEN '0' WHEN '19' THEN '0' WHEN '20' THEN '0' WHEN '21' THEN '0' WHEN '22' THEN '0' WHEN '23' THEN '0' WHEN '24' THEN '0' WHEN '25' THEN '0' WHEN '26' THEN '0' WHEN '27' THEN '0' WHEN '28' THEN '0' WHEN '29' THEN '0' WHEN '30' THEN '0' END WHERE a_question IN (16,17,18,19,20,21,22,23,24,25,26,27,28,29,30) AND a_user = '19
正如您所见,即使输出中的索引29和30等于0,即使它们在数组中有自己的值。
答案 0 :(得分:2)
您的问题在这里
$ update。= sprintf(“WHEN'%d'THEN'%d'”,$ id,$ answer);
应该是
$update .= sprintf("WHEN '%d' THEN '%s' ", $id, $answer);
答案 1 :(得分:2)
您正尝试将其格式化为%d
的整数。您应该使用%s
作为字符串:
$update .= sprintf("WHEN '%d' THEN '%s' ", $id, $answer);
答案 2 :(得分:2)
您的sprintf()
电话中的格式字符串格式错误,请使用%s
表示字符串。 %d
代表数字:
$update .= sprintf("WHEN '%d' THEN '%s' ", $id, $answer);