:sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad: :sad:

Recommended Answers

All 21 Replies

PHP problems, Seth?

you don't know the half of it : (

Well I've got a beer in front of me, all I need it a sad story so I can start crying into it:D

As my old grandmother used to say - "a problem shared is a problem halved" - so come on, let's have it!

for some reason my grade.php is always giving me a score of zero.....

here's my code..two pages.....takeTest.php and grade.php


<html>
<head>
<title>Test Week 13 CIS32</title>
</head>
<body bgcolor = "yellow">
<center>

<?

$link = mysql_connect("localhost", "web26", "web9660!", "sere1")
or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $sql");
print("Connected successfully");
mysql_select_db("sere1", $link);

$result = "Select * from quiz1 order by rand() limit 0,3";
$result = mysql_query($result) or die(mysql_error());
$num_results= mysql_num_rows($result);
echo "<form method=post action='grade.php'>";
echo "<table border=1 bgcolor=lime>";

while ($row = mysql_fetch_array($result)){
$id = $row;
$question = $row["question"];
$ans1 = $row["ans1"];
$ans2 = $row["ans2"];
$answer = $row["answer"];


echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
echo "<tr><td>$ans1 <input type=radio name='$id' value='$ans1'></td><td>$ans2 <input type=radio name='$id' value='$ans2'></td></tr>";

}
echo "</table>";

echo "<input type='submit' value='Get Grade' name='submit'>";

echo "</form>";

?>


</center>
</body>
</html>


Here's grade.php.....


<html>
<head>
<title>Grade</title>
</head>
<body bgcolor ="yellow">
<center>

<?php

$link = mysql_connect("localhost", "web26", "web9660!", "sere1")
or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $sql");

mysql_select_db("sere1", $link);


$result = "Select * from quiz1 order by rand() limit 0,3";
$result = mysql_query($result) or die(mysql_error());


$score = 0;
$total = mysql_num_rows($result);
$finalScore = $score * 10;


while ($row = mysql_fetch_array($result))
{
$question = $result["question"];
$answer = $row["answer"];
$ans1 = $result["ans1"];
$ans2 = $result["ans2"];
$id = $result["id"];
if ($id == $answer)
{
$score++;
}

}


?>
<?
echo " you scored $finalScore<br>";

?>
</center>
</body>
</html>
Here's the link to takeTest.php

http://cisweb.bristol.mass.edu/~web2...t/takeTest.php

You've got the logic path mixed up, so you will always get a 0 score. Change:

$score = 0;
$total = mysql_num_rows($result);
$finalScore = $score * 10; // this line needs to moved

while ($row = mysql_fetch_array($result))
{
$question = $result["question"];
$answer = $row["answer"];
$ans1 = $result["ans1"];
$ans2 = $result["ans2"];
$id = $result["id"];
if ($id['value'] == $answer)
{
$score++;
}
}

to:

$score = 0;
 $total = mysql_num_rows($result);

 while ($row = mysql_fetch_array($result))
 {
 $question = $result["question"];
 $answer = $row["answer"];
 $ans1 = $result["ans1"];
 $ans2 = $result["ans2"];
 $id = $result["id"];
 if ($id['value'] == $answer)
 {
 $score++;
 }
}

 $finalScore = $score * 10; // this is where it needs to be

After a closer look, you don't seem to be collecting the posted values from the form page, and so are not comparing them to your database values at any point.

Try this:

<?php
$link = mysql_connect("localhost", "web26", "web9660!", "sere1")
or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $sql");

mysql_select_db("sere1", $link);

$result = "Select id, answer from quiz1 limit 0,3";
$result = mysql_query($result) or die(mysql_error());

$score = 0;
$total = mysql_num_rows($result);

while ($row = mysql_fetch_array($result))
{
$answer = $row["answer"];
$id = $row["id"];
if ($_REQUEST['id'] == $answer)
{
$score++;
}
}

$finalScore = $score * 10;
?>
commented: goes out of his way to help +1

Thanks again.....but I'm still getting a big fat goose egg for my score.

OK, so put a bit of debug echo into the while loop to see what values are being passed through the form, what the database is sayinh the answer is, and whether they should in fact match up.

while ($row = mysql_fetch_array($result))
{
$answer = $row["answer"];
$id = $row["id"];
// start debug output
echo "db id = $id | db answer = $answer | form answer = ".$_REQUEST['id']."<br>";
// end debug output
if ($_REQUEST['id'] == $answer)
{
$score++;
}
}

I actully did that with an echo "$answer"; earlier and they were matching up......but still gave a grade of zero.

Probably because your code was wrong at that time. What is the debug output now? And can you post the full code to the grade.php page as you now have it.

sure ..... here's grade.php

<html>
<head>
<title>Grade</title>
</head>
<body bgcolor ="yellow">
<center>

<?php

$link = mysql_connect("localhost", "web26", "web9660!", "sere1")
or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $result");

mysql_select_db("sere1", $link);


$result = "Select * from quiz1 order by rand() limit 0,3";
$result = mysql_query($result) or die(mysql_error());


$score = 0;
$total = mysql_num_rows($result);
$finalScore = $score * 10; // this line needs to moved

while ($row = mysql_fetch_array($result))
{
$question = $result["question"];
$answer = $row["answer"];
$ans1 = $result["ans1"];
$ans2 = $result["ans2"];
$id = $result["id"];
if ($id == $answer)
{
$score++;
}
echo "$answer";

}
echo "$finalScore";
?>
</center>
</body>
</html>

You haven't made any of the changes to the code I previously posted. Implement the changes I posted and it WILL work.

Noticed a typo in the code i posted. Change all instances of:

$_REQUEST['id']

to:

$_REQUEST[$id]

done.....think we might be getting somewhere now.

OK, so the page now shows the debug info correctly, and is showing that the values from the form are not being populated correctly. I've copied everything locally (including your database - you should go back and edit out your username/password from the original post) and it works fine. So there is likely a typo or missed piece of code in your script.

Post it again, exactly as you have it now, and I'll post it back with any corrections so you can copy it straight in.

<html>
<head>
<title>Test Week 13 CIS32</title>
</head>
<body bgcolor = "yellow">
<center>

<?

$link = mysql_connect("localhost", "****", "*****", "*****")
or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $sql");
print("Connected successfully");
mysql_select_db("sere1", $link);

$result = "Select * from quiz1 order by rand() limit 0,3";
$result = mysql_query($result) or die(mysql_error());
$num_results= mysql_num_rows($result);
echo "<form method=post action='grade.php'>";
echo "<table border=1 bgcolor=lime>";
while ($row = mysql_fetch_array($result)){

$id = $row;
$question = $row["question"];
$ans1 = $row["ans1"];
$ans2 = $row["ans2"];
$answer = $row["answer"];


echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
echo "<tr><td>$ans1 <input type=radio name='$id' value='TRUE'></td><td>$ans2 <input type=radio name='$id' value='FALSE'></td></tr>";

}
echo "</table>";

echo "<input type='submit' value='Get Grade' name='submit'>";

echo "</form>";

?>


</center>
</body>
</html>


<html>
<head>
<title>Grade</title>
</head>
<body bgcolor ="yellow">
<center>

<?php

$link = mysql_connect("localhost", "****", "****", "****")
or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $result");

mysql_select_db("sere1", $link);


$result = "Select * from quiz1 order by rand() limit 0,3";
$result = mysql_query($result) or die(mysql_error());


$score = 0;
$total = mysql_num_rows($result);

while ($row = mysql_fetch_array($result))
{
$answer = $row["answer"];
$id = $row["id"];
// start debug output
echo "db id = $id | db answer = $answer | form answer = ".$_REQUEST."<br>";
// end debug output
if ($_REQUEST == $answer)
{
$score++;
}
}
$finalScore = $score * 10; // this line needs to moved
echo "$finalScore";
?>
</center>
</body>
</html>

Replace grade.php with this:

<?php

$link = mysql_connect("localhost", "****", "****", "****")
or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $result");

mysql_select_db("sere1", $link);


$result = "Select * from quiz1 order by  rand() limit 0,3"; 
$result = mysql_query($result) or die(mysql_error());


$score = 0;
$total = mysql_num_rows($result);

while ($row = mysql_fetch_array($result))
{
$answer = $row["answer"];
$id = $row["id"];
// start debug output
echo "db id = $id | db answer = $answer | form answer = ".$_REQUEST[$id]."<br>";
// end debug output
if ($_REQUEST[$id] == $answer)
{
$score++;
}
} 
$finalScore = $score * 10; // this line needs to moved
echo "$finalScore";
?> 
</center>
</body>
</html>

There were quote marks around the $id var in the $_REQUEST call.

Replace grade.php with this:

<?php

$link = mysql_connect("localhost", "****", "****", "****")
or die("Error ".mysql_errno().": ".mysql_error()."<br>\nThe SQL sent was: $result");

mysql_select_db("sere1", $link);


$result = "Select * from quiz1 order by  rand() limit 0,3"; 
$result = mysql_query($result) or die(mysql_error());


$score = 0;
$total = mysql_num_rows($result);

while ($row = mysql_fetch_array($result))
{
$answer = $row["answer"];
$id = $row["id"];
// start debug output
echo "db id = $id | db answer = $answer | form answer = ".$_REQUEST[$id]."<br>";
// end debug output
if ($_REQUEST[$id] == $answer)
{
$score++;
}
} 
$finalScore = $score * 10; // this line needs to moved
echo "$finalScore";
?> 
</center>
</body>
</html>

There were quote marks around the $id var in the $_REQUEST call.

Lafin Boy RULES! Thank you very much for your help!

Happy to help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.