dsfovbm931034814 2015-10-05 15:22
浏览 87
已采纳

获取循环中的所有数组值

So here's my code in getting an input from a user on how many questions (multiple choice) he/she would like to make:

       Multiple choice: <input type = "text" name="MC"><br>

       <input type = "submit" name = "confirm" value = "Confirm">

After that, this is the code of how many questions the system will generate:

<?php

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

$MC = $_POST['MC'];
echo "<form method = 'POST' name = 'items' action ='createquestions.php'>";
$items = 1;

    for ($x = 1; $x <= $MC; $x++) {

        echo "Question Number $items:"; echo "<input type = 'text' name = 'questions[]' style='width: 500px'><br><br>";
        echo "A. "; echo "<input type = 'text' name = 'ans1[]'>";
        echo "B. "; echo "<input type = 'text' name = 'ans2[]'><br>";
        echo "C. "; echo "<input type = 'text' name = 'ans3[]'>";
        echo "D. "; echo "<input type = 'text' name = 'ans4[]'><br>";
        echo "Correct Answer: "; echo "<input type = 'text' name ='cans[]'><br><br>";
        $items++;

    }
        echo "<input type ='submit' name = 'save' value = 'Save'>";
        echo "</form>";
}
?>
<?php

The problem is that it will only save the last input of the user. For example, I have inputted 2 in the Multiple choice: --textbox here-- This code will generate 2 questions, 8 choices, 2 cans = correct answer but it will only save the 2nd question, answers, and the correct answer. the system won't get the record of the 1st question, answer, and the correct answer.

Here is the code where I would insert it on the database:

<?php

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

        $user_id = $_SESSION['id'];
        $questions = $_POST['questions']; 
        $ans1 = $_POST['ans1'];
        $ans2 = $_POST['ans2'];
        $ans3 = $_POST['ans3'];
        $ans4 = $_POST['ans4'];
        $cans = $_POST['cans'];


        foreach($questions as $q){
            echo "<input type = 'hidden' value = '$q'>";
        }

        require_once('xcon.php');

        $query = "INSERT INTO mcq (mc_id, user_id, questions, ans1, ans2, ans3, ans4, cans) 
              VALUES ('NULL','$user_id','$q','$ans1','$ans2','$ans3','$ans4','$cans')";
    $result = mysql_query($query);

    if($result){
        echo 'Insert Success!';
    }
    else{
        echo 'Error';
    }

}

?>
  • 写回答

3条回答 默认 最新

  • douzhixun8393 2015-10-05 15:33
    关注

    When you save you should be running through a loop again. Try this maybe?

    <?php
    
        if(isset($_POST['save'])){
            $user_id = $_SESSION['id'];
            require_once('xcon.php');
            foreach ($_POST['questions'] as $key => $question){
                $ans1 = $_POST['ans1'][$key];
                $ans2 = $_POST['ans2'][$key];
                $ans3 = $_POST['ans3'][$key];
                $ans4 = $_POST['ans4'][$key];
                $cans = $_POST['cans'][$key];
    
    
                echo "<input type = 'hidden' value = '$question'>";
    
                $query = "INSERT INTO mcq (mc_id, user_id, questions, ans1, ans2, ans3, ans4, cans) 
                      VALUES ('NULL','$user_id','$question','$ans1','$ans2','$ans3','$ans4','$cans')";
                $result = mysql_query($query);
    
                if($result){
                    echo 'Insert Success!<br>';
                }else{
                    echo 'Error<br>';
                }
            }    
        }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作