0

:mad:


I'm currently creating a quiz program that radomly generates questions from a table in mysql........right now I have to php forms.

takeTest.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);

$sql = "Select * from quiz1 order by rand() limit 0,1";
$result = mysql_query($sql) or die(mysql_error());
$num_results= mysql_num_rows($result);
echo "<form method=post action='grade.php'>";
echo "<table border=1>";
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>


grade.php....

<?
$id = $_POST;
$score = 0;
while ($result = mysql_fetch_array($result)){
$answer = $result["answer"];
$id = $result["id"];

if ($$id == $answer)
{
$score = $score + 10;
}
}

echo "$score";

?>


I can't for the life of me figure out why I'm getting this error....any help would be a godsend.

BTW...There's only one question in the table right now...

3
Contributors
10
Replies
11
Views
10 Years
Discussion Span
Last Post by SethR
0

thanks, but limit 0,1 is correct. If I change it to limit 1,1 it won't display the questions. Right now I have three questions in my table so limit is now limit 0,3. All three questions get displayed randomly, the problem is with my grade.php file.

0

thanks, but limit 0,1 is correct. If I change it to limit 1,1 it won't display the questions. Right now I have three questions in my table so limit is now limit 0,3. All three questions get displayed randomly, the problem is with my grade.php file.

I dont think you're connected to the database in grade.php unless you're including that file from somewhere?
YOu need to create a valid mysql connection.

0

Overlooked this, but he's right. As you submit your form to grade.php, your connection is lost. You need to reconnect to the database

0

I altered the grade.php page so If I get a valid connection it print connected sucessfully....so I grade.php is connecting properly. Any other suggestions?


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

$score = 0;

$num_results= mysql_num_rows($result);
while ($result = mysql_fetch_array($result)){

if ($id == $answer)
{
$score = $score + 10;
}

echo "$score";
}


?>

0

Hello SethR,

Are you including grade.php into takeTest.php?

Assuming that you arent:

You have to have the mysql_query() and mysql_fetch_array() on the same page since mysql_fetch_array() takes the mysql_query() result as an argument.
The mysql_fetch_array() result resource does not exist in your case since this is a new mysql database link.

0

ok....I have two pages. takeTest.php and grade.php.....when I click submit to get a grade it tells me that I connected successfully but I have no database selected. Here's takeTest.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.....


<?

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

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


$score = 0;

while ($row = mysql_fetch_array($result)){


if ($id == $answer)
{
$score = $score + 10;
}

echo "$score";

}

?>

Here's the link to takeTest.php

http://cisweb.bristol.mass.edu/~web26/week13assignment/takeTest.php

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.