Jump to content

Can NOT add result in Database


zed420

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.