1

我正在为多项选择题开发一个页面。用户只能为每个问题选择一个答案。我似乎无法检索单选按钮的值(1 表示正确答案)。

这是我使用的两个表的结构

CREATE TABLE IF NOT EXISTS `question` (
`q_id` int(10) NOT NULL AUTO_INCREMENT,
`q_qstn_no` int(11) NOT NULL,
`q_text` varchar(300) NOT NULL,
`q_chpt` int(11) NOT NULL,
PRIMARY KEY (`q_id`)
)
CREATE TABLE IF NOT EXISTS `answer` (
`a_id` int(6) NOT NULL AUTO_INCREMENT,
`q_id` int(10) NOT NULL,
`a_text` varchar(255) NOT NULL,
`a_value` tinyint(1) NOT NULL,
PRIMARY KEY (`a_id`)
)

包含无线电组的 HTML 表单。

    <?php
    ini_set('display_errors',1);
    error_reporting(E_ALL ^ E_NOTICE);
    session_start();
    if(isset($_SESSION['tf1_sid']))
    {
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    <form id="form1" name="form1" method="post" action="ques_page_calc1.php">
    <p>Questions</p>
    <table width="900" border="0" cellpadding="4">
    <?php
    // db connect
    include("dbconn.php");
    // db query for questions
    $sql_q  = "SELECT q_id, q_qstn_no, q_text FROM question";
    $query_q = mysql_query($sql_q) or die("MySQL Error: " . mysql_error());
    // start loop for questions
    $rad2 = 0;
    while($data_q = mysql_fetch_array($query_q, MYSQL_ASSOC)){

    //        echo "<pre>";
    //        print_r($data_q);
    //        echo "</pre>";
    //        


        echo "<tr><td width='25' align='center' valign='top'><label><input name='q_no' size='1' type='hidden' value=". $data_q['q_qstn_no'] .">". $data_q['q_qstn_no'] ."</label></td>";
        echo "<td>". $data_q['q_text'] ."<br />";
        // db query for answers
        $sql_a = "SELECT a_id, a_text, a_value FROM answer WHERE q_id=".$data_q['q_id'];
        $query_a = mysql_query($sql_a) or die("MySQL Error: " . mysql_error());
        //$rad = 0;
        // start loop for answers
        while($data_a = mysql_fetch_array($query_a, MYSQL_ASSOC)){
            echo "<br /><table width='750' border='0'>";
            echo "<tr><td><label><input name='answer_".$rad2."' type='radio' value=". $data_a['a_value'] .">". $data_a['a_text'] . "</label></td></tr>";
            echo "</table>";
            //$rad++;
        }
        echo "</tr>";
        $rad2++;
    }
    echo "<tr><td><input name='Submit' type='submit' onClick='ques_page_calc1.php' value='Submit'></td></tr>";

    mysql_free_result($query_q);
    mysql_free_result($query_a);
    include("dbconn.php");

    ?>
    </table>
    </form>
    </body>
    </html>
    <?php
    }
    else
    {
    header("Location:s_login.php");
    }
    ?>

获取所选值的 PHP 文件

    <?php
    ini_set('display_errors',1);
    error_reporting(E_ALL ^ E_NOTICE);
    // include db connection file
    include("dbconn.php");
    session_start();

    if(isset($_POST['Submit']))
    {   
$id = $_SESSION['tf1_sid'];
echo $id;
$correct = 0;
$ev_id = 1;

//db query to obtain i_id
$sql_i = "SELECT i_id FROM ins_stud WHERE s_id = '$id'";
$query_i = mysql_query($sql_i) or die("MySQL Error: " . mysql_error());
$data_i = mysql_fetch_array($query_i, MYSQL_ASSOC);
print_r($data_i);

// capture values from HTML form
if(!empty($_POST['answer_'.$rad2]))
{   
    foreach(($_POST['answer_'.$rad2]) as $ans)
    {
    echo $ans;
    var_dump($_POST);

    if($ans == 1)
        $correct = $correct + 1;
    }

    //echo $correct;

    // insert answer to table
    //$sql_eval = "INSERT INTO eval_set (ev_id, q_id, response, created) VALUES ('" . $ev_id . "', '" . $ques_no . "', '" . $ans . "', CURDATE())";
    //mysql_query($sql_eval) or die ("Error: " . mysql_error());
    //}
    }
    //  
    // insert result to table
    //$sql_result = "INSERT INTO result (r_score, ev_id, s_id, i_id) VALUES ('" . $correct . "', '" . $ev_id . "',  '" . $id . "',  '" . $data_i . "')";
    //mysql_query($sql_result) or die ("Error: " . mysql_error());

    //echo "Result: " . $correct . " questions correct.";   

    //header("Location:ass_result.php"); 
    }

    // close db connection
    mysql_close($dbconn);
    ?>

我认为这$_POST['answer_'.$rad2]是导致问题的原因,因为我不确定如何将变量连接到名称字段。但是现在这种情况发生了变化,PHP 文件中仍然没有超出print_r($data_i);行的输出。

4

2 回答 2

0

我解决了它:) 我拿走了 if(!empty...) 并用这些替换它。

    for($i=1;$i<=$qno;$i++){
    $repStr = str_replace("1", $i, "answer_1");
    //echo "Question ". $i .": ". $repStr;
    $ans = $_POST[$repStr];
    //echo "". $radio ."<br>";

    if($ans == 1)
    $correct = $correct + 1;

    // everything before is FIXED :D

    // insert answer to table
    $sql_eval = "INSERT INTO eval_set (ev_id, q_id, response, created) VALUES ('MAX(ev_id)+1 ', '" . $i . "', '" . $ans . "', CURDATE())";
    mysql_query($sql_eval) or die ("Error: " . mysql_error());      
    }
于 2013-03-28T05:15:59.120 回答
0

您不需要为您的单选组按钮指定不同的名称。您的所有选择都将有一个名称(例如“答案”),您的 PHP 脚本将简单地检查

$_POST['answer']

这将为您提供所选单选按钮的值。因此,无论您对某个问题有多少单选按钮,都给它们一个相同的名称,就可以了。这也将确保只能检查彼此相关的单选按钮之一。

于 2013-03-20T22:44:19.890 回答