/    Sign up×
Community /Pin to ProfileBookmark

Whats wrong with my PHP code?

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 :

<?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>

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@Mr__E__CrypticFeb 24.2009 — have you tried changing:

[code=php]<?php
$query = "SELECT * FROM online_test where vertical_id=".$vert ;
?>[/code]


to:

[code=php]<?php
$query = "SELECT * FROM online_test WHERE vertical_id='" . $vert . "'" ;
//Or simply:
$query = "SELECT * FROM online_test WHERE vertical_id='$vert'" ;
?>[/code]
Copy linkTweet thisAlerts:
@shahamitauthorFeb 25.2009 — have you tried changing:

[code=php]<?php
$query = "SELECT * FROM online_test where vertical_id=".$vert ;
?>[/code]


to:

[code=php]<?php
$query = "SELECT * FROM online_test WHERE vertical_id='" . $vert . "'" ;
//Or simply:
$query = "SELECT * FROM online_test WHERE vertical_id='$vert'" ;
?>[/code]
[/QUOTE]




I have tried both the way...but it is not working.....
×

Success!

Help @shahamit spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...