我遇到一个小问题,我想在数据库中检查问题的答案,
我正在根据传入的问题ID进行查询并将答案设置为变量,然后我想将变量与用户输入进行比较;我犯了一个愚蠢的错误,但无法理解。
更新#1:
无论输入什么,它只是显示错误
我也尝试将它们与!=
<?php
// establishing the MySQLi connection
require_once('connection-test.php');
$mysqli = new mysqli($host,$username,$password,$database);
if($mysqli -> connect_error)die($mysqli->connect_error);
$questionID = $_POST['id'];
$dbAnswer = "SELECT answers FROM answers where questionID='$questionID'";
$answer = mysqli_real_escape_string($mysqli,$_POST['answer']);
if ($result3 = $mysqli->query($dbAnswer)) {
/* fetch object array */
while ($obj = $result3->fetch_object()) {
//printf ("%s \n", $obj->answers);
$ans = $obj->answers;
echo $ans;
}
if($ans != $result3){
echo "<b>error</b>" . $answer;
}else{
echo "<br><br><br>";
echo "Correct" . $answer;
}
/* free result set */
$result3->close();
}
?>
答案 0 :(得分:0)
我已修复了我的问题,现在一切正常,这是我使用的代码
// establishing the MySQLi connection
require_once('connection-test.php');
$mysqli = new mysqli($host,$username,$password,$database);
if($mysqli -> connect_error)die($mysqli->connect_error);
$questionID = $_POST['id'];
$userAnswer = $_POST['answer'];
$query = "SELECT answers FROM answers WHERE questionID=?";
$statement = $mysqli->prepare($query);
$statement ->bind_param('i', $questionID);
$statement->execute();
$statement->bind_result($answer);
while($statement->fetch()){
if($answer != $userAnswer){
echo "Wrong";
}else{
echo "correct!";
}
}
$statement->close();