Andreycon Posted September 4, 2020 Share Posted September 4, 2020 I am playing around with php but the script doesnt execute to the last point and with no error... What i am trying to do is checking the input of the user and comparing it what is in db and then applying conditions. So i have 2 files. update.php and home.php. home.php is my html form page and i included 'update.php' in it In my update.php, i have these codes <?php error_reporting(E_ALL); include_once('database.php'); if(isset($_POST['submit'])) { $q = $_GET['q']; $next = $q + 1; if($q == 26) { echo '<h2>You have Completed the Exam.Congrats!!</h2>'; } elseif($q <= 25){ $sql = "SELECT * FROM Students WHERE Email=?"; $stmt = $conn->prepare($sql); $stmt->bind_param('s', $_SESSION['login']); $stmt->execute(); $result = $stmt->get_result(); $row = $result->fetch_assoc(); $Mark = $row['Mark']; $Correct = $Mark + 1; $Fail = $Mark + 0; $sqlb = "SELECT * FROM Answers WHERE qid = ?"; $stmtb = $conn->prepare($sqlb); $stmtb->bind_param('s', $q); $stmtb->execute(); $resultb = $stmtb->get_result(); $rowb = $resultb->fetch_assoc(); $Answer = $rowb['Answer']; $Pick = $_POST['ans']; if($Pick == $Answer) { $sqld = "UPDATE Students SET Mark=? WHERE Email = ?"; $stmtd = $conn->prepare($sqld); $stmtd->bind_param('ss', $Correct,$_SESSION['login']); $stmtd->execute(); $resultd = $stmtd->get_result(); echo "correct" ; } } else { echo 'Ate'; echo "<script type='text/javascript'>document.location.href='home.php?q='.$next.';</script>"; } }//post submit ?> The else statement doesnt run. so it keeps reloading the same page. Also i want to ask why i keep getting the value of 2 for my $Correct variable stored on the database into column Mark for a single correct question instead of 1. What could be wrong? Lastly, i dont want the script to run if the browser is edited by the user by changing the value of $q from the browser, because i am using get method, is there a way to do that. Please be nice with your comments. Thanks in advance.!!! Quote Link to comment https://forums.phpfreaks.com/topic/311429-code-not-executing-beyond-a-point/ Share on other sites More sharing options...
requinix Posted September 4, 2020 Share Posted September 4, 2020 First step is for you to learn how to indent your code properly. I'm not entirely sure but I suspect your problem will become apparent if you do this. Quote Link to comment https://forums.phpfreaks.com/topic/311429-code-not-executing-beyond-a-point/#findComment-1581143 Share on other sites More sharing options...
Andreycon Posted September 4, 2020 Author Share Posted September 4, 2020 n applying conditions. So i have 2 files. update.php and home.php. home.php is my html form page and i included 'update.php' in it In my update.php, i have these codes <?php error_reporting(E_ALL); include_once('database.php'); if(isset($_POST['submit'])) { $q = $_GET['q']; $next = $q + 1; if($q == 26) { echo '<h2>You have Completed the Exam.Congrats!!</h2>'; } elseif($q <= 25){ $sql = "SELECT * FROM Students WHERE Email=?"; $stmt = $conn->prepare($sql); $stmt->bind_param('s', $_SESSION['login']); $stmt->execute(); $result = $stmt->get_result(); $row = $result->fetch_assoc(); $Mark = $row['Mark']; $Correct = $Mark + 1; $Fail = $Mark + 0; $sqlb = "SELECT * FROM Answers WHERE qid = ?"; $stmtb = $conn->prepare($sqlb); $stmtb->bind_param('s', $q); $stmtb->execute(); $resultb = $stmtb->get_result(); $rowb = $resultb->fetch_assoc(); $Answer = $rowb['Answer']; $Pick = $_POST['ans']; if($Pick == $Answer) { $sqld = "UPDATE Students SET Mark=? WHERE Email = ?"; $stmtd = $conn->prepare($sqld); $stmtd->bind_param('ss', $Correct,$_SESSION['login']); $stmtd->execute(); $resultd = $stmtd->get_result(); echo "correct" ; } } else { echo 'Ate'; echo "<script type='text/javascript'>document.location.href='home.php?q='.$next.';</script>"; } }//post submit ?> Is rhe code clearer now? Quote Link to comment https://forums.phpfreaks.com/topic/311429-code-not-executing-beyond-a-point/#findComment-1581145 Share on other sites More sharing options...
Andreycon Posted September 4, 2020 Author Share Posted September 4, 2020 I checked the code again and saw the problem. I was adding 1 to $Mark to get $Correct. So if $Mark == 1, $Correct == 2... Quote Link to comment https://forums.phpfreaks.com/topic/311429-code-not-executing-beyond-a-point/#findComment-1581146 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.