php - 在数据库中插入单选按钮输入

标签 php mysql phpmyadmin

我正在创建一个类似测验的结构。为此,我试图获取每个问题的答案,并将其与已回答的问题类型一起插入到我的数据库中。但是,我在将这些数据放入变量时遇到问题,我得到: 注意:未定义索引:question-0-answer

在代码的注释部分。任何帮助..

$options = '';
$filter=mysql_query("select afnumber from employees WHERE Status='Employed '");
while($row = mysql_fetch_array($filter)) {
    $options .="<option >" . $row['afnumber'] . "</option>";
}
$menu="<form id='filter' name='filter' method='post' action=''>
AFNumber : <select name='SelectAF' id='filter' style='color:grey;'>" . $options . "</select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Evaluation Test Type : <select name='Type' id='type' style='color:grey;'><option selected='selected'></option><option value='loyalty'>Loyalty</option><option value='performance'>Performance</option></select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type='submit' name='submit1' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>
</form>
<br>
";
 echo $menu;

if(isset($_POST['submit1']))

{   
$type = $_POST['Type'];

$mysqli = new mysqli("localhost", "root", "Js", "jr");
/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

 if ( $result = $mysqli->query( "SELECT questiontext FROM questioninfo WHERE type='$type'" ) ) {


        $html=array();

        $html[]="
        <form action='' method='post' id='quiz'>
            <ol>";

        $counter=1;

        while( $row = $result->fetch_array() ) {


            $question=$row['questiontext'];
            $answerA=1;
            $answerB=2;
            $answerC=3;
            $answerD=4;
            $answerE=5;

            $html[]="
             <br/>
                <h3>Question {$counter}:&nbsp; {$question}</h3>

                <li>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersA' value='A' />
                    <label for='question-{$counter}-answers-A'> {$answerA} </label>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersB' value='B' />
                    <label for='question-{$counter}-answers-B'> {$answerB} </label>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersC' value='c' />
                    <label for='question-{$counter}-answers-C'> {$answerC} </label>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersD' value='D' />
                    <label for='question-{$counter}-answers-D'> {$answerD} </label>
                    <br/>
                    <input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersE' value='E' />
                    <label for='question-{$counter}-answers-E'> {$answerE} </label>

                </li>";

            $counter++;

        }

        $html[]="
            </ol>
        <input type='submit' name='submit' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>
        <input type='hidden' name='type' value='{$type}' />
        </form>";

        echo implode( PHP_EOL, $html );



    $result->close();

 }
}
/*
if( isset( $_POST['submit'] ) ){ 


$numQuestions=10;

for( $counter=0; $counter < $numQuestions; $counter++ ){ 
$answer=$_POST['question-'.$counter.'-answers']; 
$sql="insert into `question` (`type`,`value`) values ('".$type."','".$answer."')"; 
$db->query( $sql ); 
} 
}
*/

最佳答案

用于输出单选按钮的 $counter 使用值 1 进行初始化,但是在脚本末尾读取单选按钮的值时,您将从 开始>$counter = 0;.

请更改

for( $counter=0; $counter < $numQuestions; $counter++ ){

for( $counter=1; $counter <= $numQuestions; $counter++ ){ 

关于php - 在数据库中插入单选按钮输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32043552/

相关文章:

PHP从子窗口向父窗口传递数据

php - 在 php 中传递 MySql 连接

mysql - #1064 - 您的 SQL 语法有错误 : What is wrong with this query?

mysql - 有没有办法优化 MYSQL 中的 `SHOW DATABASES` 查询

mysql - 在 Homestead 盒子上设置 phpmyadmin

javascript - php将pdf文件打印到指定的网络打印机而不打开对话框

java - 无法转换 getSingleResult() 的结果

MySQL : LEFT JOIN question, 返回太多记录

php - Fusionchart : Some String Chracters Not Display In Fusion Pie Chart

PHP password_verify 不适用于数据库