无法在php中保存textarea的多个用户输入

时间:2017-05-09 17:01:54

标签: php html mysql

我从lvl2itquepaper检索数据并用textarea显示它并将用户输入保存到另一个表调用lvl2itresult但是当我按save时,它只保存最后一个用户输入。 例如,2个问题有2个文本区域,第1个文本区域是' A'第二个文本区域是' B'它将用户输入保存为' B'

             <?php
               include('../dbconnect.php');
              session_start();
             ?>



                <!DOCTYPE html>
           <html>

              <head>
                   <title>Online Examination System</title>
              </head>
            <body>


    <div id="container">
    <h1>Level 2 IT Question Paper</h1>
    <h2>Please read the question carefully and answer it confidently. Good Luck All!</h2>


        <?php
        if(isset($_POST['Submit']))
        {
            $sql="SELECT * from lvl1itquepaper";
        $run_que = mysqli_query($mysqli, $sql);
        $check_que = mysqli_num_rows($run_que);

            while ($row=$run_que->fetch_assoc())
            {
                $questionno = $row['questionno'];
                $question = $row['question'];
                $student_ans = $_POST['studentans'];


                $sql="insert into lvl2itresult (questionno, question, studentans, username) values ('.$questionno.', '$question', '$student_ans', '".$_SESSION['login_user']."')";

                $submit = $mysqli->query($sql);


            }            
        }
        ?>




        <form method= "post">
    <?php
        echo "Welcome, ";
        $sql="SELECT * from lvl2itstudent WHERE username= '".$_SESSION['login_user']."'";
        $find_student = mysqli_query($mysqli, $sql);
        $check_student = mysqli_num_rows($find_student);
            if ($check_student>0){
                while($row = $find_student->fetch_assoc())
                {
                    echo $row['username'];
                }
            }
        echo "<br><br><br><br>";

        $sql="SELECT * from lvl2itquepaper";
        $run_que = mysqli_query($mysqli, $sql);
        $check_que = mysqli_num_rows($run_que);

        if($check_que>0){
            while ($row=$run_que->fetch_assoc())
            {
                $questionno = $row['questionno'];
                $question = $row['question'];
                echo "".$questionno. "." .$question."<br>";
                echo "<textarea name='studentans' rows='5' cols='50'></textarea><br><br>";



            }
        }

             else {
            echo "there is no data in database";
        }
            ?>
            <input type="submit" value = "Submit" name= "Submit" style= "width:60px; height:30px";>

        </form>
    </div>
</body>

1 个答案:

答案 0 :(得分:2)

这很简单,但很难用几句话来解释。由于没有设置数组,因此在POST数据中只发送了一个参数,这几乎是最重要的。此外,PHP也没有将信息视为数组,因此只能解释一个值。在任何情况下,如果数据发送正确,PHP会将POST值标识为2维数组,输出SQL错误,因为无法将数据解析为字符串。

要解决此问题,请先按照以下步骤更改<textarea>标记:

<textarea name='studentans[]' rows='5' cols='50'></textarea>

括号将告诉HTML,会有许多名称为studentans的元素。

现在进入插入逻辑,您需要使用索引处理数组获取值。通常这些将从0开始,所以我们将使用它:

$i = 0; // $i stands for index.
while ($row=$run_que->fetch_assoc()){
    $questionno = $row['questionno'];
    $question = $row['question'];
    $student_ans = $_POST['studentans'][$i]; // this is where the magic happens

    $sql="insert into lvl2itresult (questionno, question, studentans, username) values ('$questionno', '$question', '$student_ans', '".$_SESSION['login_user']."')";
    // please also notice I deleted 2 concatenating dots near "values ('$questionno',"
    $submit = $mysqli->query($sql);
    $i++; // increment by 1 so we can access the next value on the next loop
}

那应该这样做。我希望我没有忘记任何细节。