如何将sql查询存储到变量以将1添加到其当前值。我已经有了一个代码但它没有在select查询['eCorrect']中添加1当前值,而是在提交表单后结果为2。并且它显示此错误类mysqli_result的对象无法转换为int。我已将'eCorrect'字段设置为数据类型INT。我不知道我的错误在哪里请提前帮助谢谢。
<form method="post" action="elQ2.php">
<p>Question: sample question sample question sample question sample question</p>
<input type="radio" name="Ctwo" value="wrong">Choice A<br>
<input type="radio" name="Ctwo" value="wrong">Choice B<br>
<input type="radio" name="Ctwo" value="correct">Choice C<br>
<input type="radio" name="Ctwo" value="wrong">Choice D<br>
<input type="submit" name="submit" value="Submit" class="pull-right">
</form>
</div>
</div>
这是我的PHP代码
<?php
$correctResult = $db->query("SELECT eCorrect FROM elearn WHERE eID=1");
$wrongResult = $db->query("SELECT eWrong FROM elearn WHERE eID=1");
$addC = $correctResult;
$addW = $wrongResult;
if(isset($_POST['submit'])) {
if(!empty($_POST['Ctwo'])){
if($_POST['Ctwo']=="correct") {
$eAns = $_POST['Ctwo'];
$eAns= $addC + 1 ;
if($update = $db->query(" UPDATE elearn SET eCorrect = '".$eAns."' WHERE eID=1 ")) {
}else{
die($db->error);
}
echo "Correct Answer";
}else{
//Wrong answer
$eAns = $_POST['Ctwo'];
$eAns = $addW + 1 ;
if($update = $db->query(" UPDATE elearn SET eWrong = '".$eAns."' WHERE eID=1 ")) {
}else{
die($db->error);
}
echo "Wrong answer";
}
} else{
echo "You must choose an answer";
}
}
?>
答案 0 :(得分:0)
出现问题是因为$ addC和$ addW变量包含结果集。实际上,您正在为结果集添加+1。
尝试改为:
$cRow = $db->query("SELECT eCorrect FROM elearn WHERE eID=1");
$correctResult = $cRow[0];
$wRow = $db->query("SELECT eWrong FROM elearn WHERE eID=1");
$wrongResult = $cRow[0];