zed420 Posted July 7, 2008 Share Posted July 7, 2008 Hi Everyone I'm trying to create a quiz on line so far so good. When it comes to add the user's results in database it displays 0, I just can not understand why??? There are no errors, id and name insert into database without a problem but Bloody results I can not figure out, Can anyone help please. include("config.php"); $query = "SELECT * FROM php_tb ORDER BY id"; $result = mysql_query($query) or die ("Couldn't execute query 2."); if (!$_POST['submit']) { echo "<form method=post action=$PHP_SELF>"; while ($row = mysql_fetch_array($result)){ $id= $_POST["id"]; $question= $_POST["question"]; $opt1= $_POST["opt1"]; $opt2= $_POST["opt2"]; $opt3= $_POST["opt3"]; $answer= $_POST["answer"]; } } elseif ($_POST['submit']){ $score = 0; $total = mysql_num_rows($result); while ($row = mysql_fetch_array($result)){ $answer = $row[answer]; $z = "q$row[id]"; $z = trim($z); if ($_POST[$z] == $answer) { $score++; } } echo "<p align=center><b>You scored $score out of $total</b></p>"; echo "<p>"; if ($score == $total) { echo "Congratulations! You got all the question right!"; echo "<p>Well done $name, with a score of $score </p>"; ?></h1><? } elseif ($score/$total < 0.34) { echo "Don't bother trying again $name you have failed badly... go do some thing else"; } elseif ($score/$total > 0.67) { echo "Well done! You certainly know your stuff but you could have done better."; } else { echo "Not bad - but it could better!"; } $id = $row["id"]; $name = $_SESSION['name']; $score = $_POST["score"]; $Query="INSERT into result (id,name,score) values ('$id','$name' , '$score')"; if (@mysql_query($Query)) { echo '<p>Your New Information has been added. </p>'; } else { echo '<p>Error adding submitted Information: ' . mysql_error() . '</p>'; } } } mysql_close ($db); ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/113592-can-not-add-result-in-database/ Share on other sites More sharing options...
discomatt Posted July 7, 2008 Share Posted July 7, 2008 Here's a hint... echo $_POST['score']; $score = $_POST["score"]; Link to comment https://forums.phpfreaks.com/topic/113592-can-not-add-result-in-database/#findComment-583645 Share on other sites More sharing options...
gigas10 Posted July 7, 2008 Share Posted July 7, 2008 where exactly are you posting score? $score = $_POST["score"]; this line makes no sense. Link to comment https://forums.phpfreaks.com/topic/113592-can-not-add-result-in-database/#findComment-583648 Share on other sites More sharing options...
zed420 Posted July 7, 2008 Author Share Posted July 7, 2008 Thanks for quick reply I've just changed it to $score = $row["score"]; . It hasn't made any difference. To be quite honest I'm well lost. Link to comment https://forums.phpfreaks.com/topic/113592-can-not-add-result-in-database/#findComment-583696 Share on other sites More sharing options...
ag3nt42 Posted July 7, 2008 Share Posted July 7, 2008 after you calucate the scores... your setting $score back to = $_POST['score'] I bet $_POST['score']=0 id = $row["id"]; $name = $_SESSION['name']; $score = $_POST["score"];//this line wat happens if you take that line out Link to comment https://forums.phpfreaks.com/topic/113592-can-not-add-result-in-database/#findComment-583709 Share on other sites More sharing options...
zed420 Posted July 7, 2008 Author Share Posted July 7, 2008 ag3nt42 just like to say thank you blue. I took that line out and got the result I wanted. It worked ;D ;D Link to comment https://forums.phpfreaks.com/topic/113592-can-not-add-result-in-database/#findComment-583975 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.