Wrox Programmer Forums
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 15th, 2007, 01:43 AM
Registered User
 
Join Date: Feb 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help needed

Hello. i am new to this php. for some reason my script keeps giving me error undefined variable. here is my quiz1.php script
Code:
<?php

//copyright widgetmonkey.com 2001

include("contentdb.php");

$display = mysql_query("SELECT * FROM $table ORDER BY id",$db);

if (!$submit) {


    echo "<form method=post action=$PHP_SELF>";
    echo "<table border=0>";

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

    $id = $row["id"];
    $question = $row["question"];
    $opt1 = $row["opt1"];
    $opt2 = $row["opt2"];
    $opt3 = $row["opt3"];
    $answer = $row["answer"];

    echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
    echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>";

    }

    echo "</table>";
    echo "<input type='submit' value='See how you did' name='submit'>";
    echo "</form>";

}

elseif ($submit) 

{

    $score = 0;
    $total = mysql_num_rows($display);
        while ($result = mysql_fetch_array($display)) 


        {

            $answer = $result["answer"];
            $q = $result["q"];

        if ($$q == $answer) 
        {
        $score++; 
        }

    }

    echo "<p align=center><b>You scored $score out of $total</b></p>";
    echo "<p>";

    if   ($score == $total) {
    echo "Congratulations! You got every question right!";
    }
    elseif ($score/$total < 0.34) {
    echo "Oh dear. Not the best score, but don't worry, it's only a quiz.";
    }
    elseif ($score/$total > 0.67) {
    echo "Well done! You certainly know your stuff.";
    }
    else {
    echo "Not bad - but there were a few that caught you out!";
    }

echo "</p>";

echo "<p>Here are the answers:";

echo "<table border=0>";
$display = mysql_query("SELECT * FROM $table ORDER BY id",$db);
while ($row = mysql_fetch_array($display)) {

$question = $row["question"];
$answer = $row["answer"];
$q = $row["q"];

echo "<tr><td><br>$question</td></tr>";

if ($$q == $answer) 
        {
        echo "<tr><td>&raquo;you answered ${$q}, which is correct</td></tr>";
        }
elseif ($$q == "") {
echo "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td></tr>";
}
else {
echo "<tr><td>&raquo;you answered ${$q}. The answer is $answer</td></tr>";
}

}
echo "</table></p>";



}
these are the errors i am getting.
Notice: Undefined variable: submit in c:\program files\easyphp1-7\www\quizv1.0\quiz1.php on line 9

Notice: Undefined variable: PHP_SELF in c:\program files\easyphp1-7\www\quizv1.0\quiz1.php on line 12

any help will be really appreciated

 
Old February 15th, 2007, 03:25 AM
Authorized User
 
Join Date: Feb 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Mike,

I think you have forgot to include another file in which for variable are defined for $submit and $PHP_SELF. Since these two variables are not defined. You are getting errors.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed ramabharti Javascript How-To 1 December 7th, 2006 06:42 AM
Help needed rrvinod ADO.NET 0 October 5th, 2006 09:44 AM
help needed! paquito Beginning PHP 2 November 16th, 2005 11:51 AM
A Little help needed matthew01 ASP.NET 1.0 and 1.1 Basics 3 November 9th, 2005 11:29 AM
help needed kfarooq Java Databases 1 June 27th, 2003 05:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.