how to store a sql query to a variable to add 1 to its current value. I already have a code but it doesn't add 1 to the current value in the select query ['eCorrect'] instead the result will be 2 after submitting the form. And it show this error Object of class mysqli_result could not be converted to int. I have set my 'eCorrect' field to data type INT. I don't know where my error is please help thanks in advance.
<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>
this is my php code
<?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";
}
}
?>
The problem occurs because the $addC and $addW variables are containing resultsets. So actually you are adding +1 to a resultset.
Try to do this instead:
$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];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.