从while循环向数据库插入多行

时间:2017-09-15 14:12:30

标签: php html mysql

美好的一天!我在数据库中存储值时遇到问题。程序的流程是用户将输入他将获得多少主题。例如,他/她可以放6个科目。因此它将释放6个输入类型,其值等于text。我的问题是,当用户输入6个输入时,我的数据库中没有6行。

Image of Front End

HTML / PHP

<form method="POST">
    <table>
        <tr>
            <td>SUBJECT CODE/SUBJECT DESCRIPTION/SEMESTER</td>
        </tr>
        <?php $counter=1 ; 
            while($counter <= $subj){?>
            <tr>
                <td>
                    <select name="sub">
                        <?php $select=mysql_query("SELECT * FROM subject 
                                                   WHERE course_code = '$course' AND semester = '$sem'"); 
                              while($rows=m ysql_fetch_assoc($select)){ 
                                   $code=$rows['subj_code']; 
                                   $desc=$rows['subj_desc']; 
                                   $units=$rows['units']; 
                                   $yr=$rows[ 'year_level'];
                        ?>
                                 <**option value="<?php echo $codes[$code]; ?>">
                                   <?php echo $code. " - ".$desc; ?>
                                 </option>**
                             <?php } ?>
                    </select>
                </td>
                <td>

                </td>
            </tr>
            <?php $counter++; } } ?>
        <tr>
            <td>
                <input type="submit" name="add" value="add subjects">
            </td>
        </tr>
    </table>
</form>

PHP / SQL

<?php
if (isset($_POST['add'])) {
    $data    = array();
    $subject = $_POST['sub'];
    $sem     = $_SESSION['sem'];

    for ($i = 0; $i < count($subject); $i++) {
        $subject = mysql_real_escape_string($subject[$i]);
        $sem     = mysql_real_escape_string($sem[$i]);
        $yr      = mysql_real_escape_string($yr[$i]);
        $fac     = mysql_real_escape_string($fac[$i]);
        $col     = mysql_real_escape_string($col[$i]);

        $set = mysql_query("SET foreign_key_checks = 0");
        if (!$set) {
            die("Foreign key SET failed");
        } else {
            $insertmid = mysql_query("INSERT INTO grade_midterm 
                                     (midterm_grade, semester, year_level, subj_code, stud_id, fac_id, col_code) 
                                      VALUES ('NA','$sem','$yr','$subject', '$user','$fac','$col')");
            if (!$insertmid) {
                die("Failed to insert midterm" . mysql_error());
            } else {
                $insertfinal = mysql_query("INSERT INTO grade_final 
                                           (final_grade, semester, year_level, subj_code, stud_id, fac_id, col_code)
                                            VALUES ('NA','$sem','$yr','$subject','$user','$fac','$col')");
                if (!$insertfinal) {
                    die("Failed to indert final");
                } else {
                    $set2 = mysql_query("SET foreign_key_checks = 1");
                    echo "<script>alert('Success Adding Subject');</script>";
                }
            }
        }
    }
}
?> 

1 个答案:

答案 0 :(得分:1)

你的循环中有一个名为sub的选择框

<select name="sub">

这意味着每个下一个选择框都会覆盖前一个选择框 使用

<select name="sub[]">

在$ _POST变量中创建一个数组