What is wrong with my PHP code?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
shahamit
Forum Newbie
Posts: 1
Joined: Tue Feb 24, 2009 6:25 am

What is wrong with my PHP code?

Post by shahamit »

Hi guys,

I am not sure how to fix this php code, so that it calculates the marks accurately.

If I write a query ( $query = "SELECT * FROM online_test"; ) the following code works fine and calculates the score accurately.
However, when i give a where clause in the query the following script does not calculates the marks accurately.

Any help will be highly appreciated

Thank you in advance


The table "online_test" contains the following feilds in the same order as mentioned below :

question
answer
option1
option2
option3
option4
Qno
vertical_id
here is the code :

Code: Select all

 
<?php
 
 
$vert=1;
mysql_connect("localhost","root","") or die("Unable to connect Server");
 
mysql_select_db("domain_test") or die( "Unable to select database");
 
$query = "SELECT * FROM online_test where vertical_id=".$vert ;
$result = mysql_query($query);
?>
<form name="test" method="POST" action="online_test.php">
<table>
<?php

$ans =array();
while ($row = mysql_fetch_array($result)) 
{
$ans[$row[6]][0] = $row[6];
$ans[$row[6]][1] = $row[1];
$rightanswer= $row[1];
printf("<tr><td style=''>Qno: %s <br></td></tr>
<tr><td>Question: %s <br></td></tr>
<tr><td>a: <input type='radio' value='%s' name='radio%s'> %s <br></td></tr> 
<tr><td>b: <input type='radio' value='%s' name='radio%s'> %s <br></td></tr> 
<tr><td>c: <input type='radio' value='%s' name='radio%s'> %s <br></td></tr> 
<tr><td>d: <input type='radio' value='%s' name='radio%s'> %s <br></td></tr>", 
$row[6], $row[0],$row[2],$row[6], $row[2], $row[3],$row[6],$row[3],$row[4],$row[6], $row[4],$row[5],$row[6],$row[5]);
}
 
?>
<tr><td><input type='submit' value='Submit' name='submit'></td></tr>
 
</table>
</form>
 
<?php
 
if(isset($_POST['submit']))
{
$marks=0;
//print_r($_POST['radio4']);
for($i=1;$i<=count($ans);$i++)
{
 
if($ans[$i][1] == $_POST['radio'.$i])
{
$marks += 1;
 
echo $marks;
}
else
{
$marks += 0;
}
}

echo "Your Mark is: ".$marks;
set_time_limit(20);
 
}
 
?>
 
 
 
<html>
<head>
 
</head>
</body>
</html>
 
Post Reply