我有四个选择,其中两个可以是正确答案。
if($isCorrect == "answer1"){
$sql2 = mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '1')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer3', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer4', '0')")or die(mysql_error());
$msg = 'You questions has been succesfully added!';
header('location: addQuestions.php?msg='.$msg.'');
exit();
}
if($isCorrect == "answer2"){
$sql2 = mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '1')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer3', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer4', '0')")or die(mysql_error());
$msg = 'You questions has been succesfully added!';
header('location: addQuestions.php?msg='.$msg.'');
exit();
}
if($isCorrect == "answer3"){
$sql2 = mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer3', '1')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer4', '0')")or die(mysql_error());
$msg = 'You questions has been succesfully added!';
header('location: addQuestions.php?msg='.$msg.'');
exit();
}
if($isCorrect == "answer4"){
$sql2 = mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer4', '1')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', '0')")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer3', '0')")or die(mysql_error());
$msg = 'You questions has been succesfully added!';
header('location: addQuestions.php?msg='.$msg.'');
exit();
}
}
当我运行这些时,只有一个被接受为正确答案。
我的数据库有什么方法可以接受两个正确的答案吗?我使用枚举(0,1)来检查正确的答案。
答案 0 :(得分:0)
有人可能会在稍后回复一个更好的答案,这只是为了让球滚动。
我首先要重构您目前所拥有的内容,如下所示:
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip({'delay': { show: 5000, hide: 3000 }
});
});
我不知道您使用// note: there are a lot of ways to make the following cleaner (use an array,
// write a function, etc.), but for the sake of time and simplicity I'll
// leave it at this
// if $isCorrect == "answer1", $answer1Correct = '1', else $answer1Correct = '0'
$answer1Correct = ($isCorrect == "answer1" ? '1' : '0');
$answer2Correct = ($isCorrect == "answer2" ? '1' : '0');
$answer3Correct = ($isCorrect == "answer3" ? '1' : '0');
$answer4Correct = ($isCorrect == "answer4" ? '1' : '0');
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', $answer1Correct)")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer2', $answer2Correct)")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer3', $answer3Correct)")or die(mysql_error());
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer4', $answer4Correct)")or die(mysql_error());
$msg = 'You questions has been succesfully added!';
header('location: addQuestions.php?msg='.$msg.'');
exit();
的内容,我在这里忽略了它。
到目前为止,这应该与您目前拥有的相同
然后,为了进行您正在寻找的更改,我会将$sql2
从字符串更改为字符串数组。
例如,假设答案1和4都是正确的: $ isCorrect = array(' answer1',' answer4');
然后您使用in_array(),以便$isCorrect
部分变为:
$answer1Correct
修改:更多改进
而不是使用三元运算符(// if "answer1" is in the $isCorrect array, set $answer1Correct to '1', otherwise set it to '0'
$answer1Correct = (in_array("answer1", $isCorrect) ? '1' : '0');
$answer2Correct = (in_array("answer2", $isCorrect) ? '1' : '0');
$answer3Correct = (in_array("answer3", $isCorrect) ? '1' : '0');
$answer4Correct = (in_array("answer4", $isCorrect) ? '1' : '0');
),您可以convert a boolean to an integer,然后将其转换为字符串。
condition ? true_value : false_value
返回整数1,(int)true
返回字符串' 1'。
所以你的$ answer1Correct部分变为: $ answer1Correct =(string)(int)in_array(" answer1",$ isCorrect); $ answer2Correct =(string)(int)in_array(" answer2",$ isCorrect); $ answer3Correct =(string)(int)in_array(" answer3",$ isCorrect); $ answer4Correct =(string)(int)in_array(" answer4",$ isCorrect);
但是复制+粘贴这么多都有点乏味,更多dry选项是用函数替换(string)(int)true
:
$answer1Correct
然后您的查询变为:
function answerIsCorrect($answerNumber, $allCorrectAnswers) {
return (string)(int)in_array("answer" . $answerNumber, $allCorrectAnswers);
}
现在你可以将整个事情作为一个循环,所以你的整个脚本可以缩短为:
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', '$answer1', answerIsCorrect(1, $isCorrect)")or die(mysql_error());
如果您将function answerIsCorrect($answerNumber, $allCorrectAnswers) {
return (string)(int)in_array("answer" . $answerNumber, $allCorrectAnswers);
}
$answers = array(
1 => $answer1,
2 => $answer2,
3 => $answer3,
4 => $answer4
)
for($i = 1; $i <= 4; $i++) {
mysql_query("INSERT INTO answers (question_id, answer, correct) VALUES ('$lastId', $answers[$i], answerIsCorrect($i, $isCorrect)")or die(mysql_error());
}
从字符串更改为字符串数组,如上所述。