dongqiang5541 2017-05-09 09:01
浏览 51
已采纳

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

I have retrieve data from lvl2itquepaper and display it with textarea and save the user input to another table call lvl2itresult but when I press save, it save the last user input only. For example, got 2 question with 2 text area, 1st text area are 'A' and 2nd text area are 'B', it save both user input as '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条回答 默认 最新

  • dousu1916 2017-05-09 09:33
    关注

    This is pretty easy, but hard to explain in a few words. What's pretty much going on is that only one parameter is being sent in the POST data since no array has been set; furthermore PHP is not treating the information as arrays either, so there's only one value to be interpreted. In any case, if the data was sent correctly, PHP would identify the POST value as an array of 2 dimensions, outputting a SQL error for not being able to parse the data as a string.

    To solve this, first change your <textarea> tag as follows:

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

    The brackets will tell HTML that there will be many elements with the name studentans.

    Now breaking into your insert logic, you need to treat the arrays getting the values using indexes. Usually these will start on 0, so we will work with that:

    $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
    }
    

    And that should do it. I hope I didn't forget any detail.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部