-2
 <?php
    $connection=mysql_connect('localhost','root',"");
    if(!$connection)
    {
        die("database connection failed".mysql_error());
    }
    $db_select=mysql_select_db("db",$connection);
    if(!$db_select)
    {
        die("database connection failed".mysql_error());
    }
    if(!isset($_POST['submit']))
    {
        echo "<form method=\"post\" action=\"test1.php\">";
        $sub_result=mysql_query("SELECT * from test where tid='1' order by rand() limit 0,2",$connection);
        if(!$sub_result)
        {
            die("database query failed". mysql_error());
        }

        while ($sub_row=mysql_fetch_array($sub_result))
        {
            $id=$sub_row["qno"];
            $ques=$sub_row["ques"];
            $opt1=$sub_row["opt1"];
            $opt2=$sub_row["opt2"];
            $opt3=$sub_row["opt3"];
            $opt4=$sub_row["opt4"];
            $ans=$sub_row["ans"];

            echo "<h3>Q".$id." :".$ques."</br></h3>";   
            echo"</br>
                $opt1
                <input type= radio name=\"{$id}\" value=\"{$opt1}\" >
                </br>
                $opt2
                <input type= radio name=\"{$id}\" value=\"{$opt2}\">
                </br>
                $opt3
                <input type= radio name=\"{$id}\" value=\"{$opt3}\">
                </br>
                $opt4
                <input type= radio name=\"{$id}\" value=\"{$opt4}\">
                </br></br>";
        }
        echo"<input type='submit' value='see how you did it' name='submit'>";
        echo"</form>";

    }


        if(isset($_POST['submit']))
        {

        $total=0;
        $answer=mysql_query("select qno,ans from test",$connection);

        while($ans=mysql_fetch_assoc($answer))
        {
            if($_POST[$ans['qno']] === $ans['ans'])
            {
                $total++;
            }
            else
            {
            }
        }
        echo"<p align=center><b>I Got $total</b>";
        }
 ?>

出现错误“第 60 行中的未定义偏移量,我正在这里验证答案。”

4

1 回答 1

0

问题是变量#set in$ans['qno']不作为数组中的对存在$_POST$_POST数组值比#set in 少$ans['qno']

将第 60 行从:

if($_POST[$ans['qno']] === $ans['ans']) {

至:

if( isset( $_POST[$ans['qno']] ) && ( $_POST[ $ans['qno'] ] === $ans['ans'] ) ) {

我们使用isset()首先确保$_POST[x]存在,并且仅当它存在时,将其与$ans['ans'].

于 2012-11-14T15:26:52.857 回答