使用foreach在一次提交时插入多个值

时间:2015-12-22 08:57:06

标签: php mysqli foreach

我正在尝试使用foreach在表格中插入多个值,但我无法正确使用

这是HTML CODE

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" id="studentresult">
            <div class="tableDiv_01 table-responsive">
                <div class="tablehHeader">
                    <span class="field col-xs-9">اسم الطلاب</span>
                    <span class="field col-xs-3 text-center">الدرجة</span>
                </div>

                <ul>
                    <?php
                    $active = 1;
                    $getStud = $db->prepare('SELECT
                    a.id, a.std_id, a.subDegree,
                    b.id, b.name
                    FROM student_subjects AS a
                    INNER JOIN student_basic_info AS b ON (a.std_id = b.id)
                    WHERE a.sub_id=? AND a.activitySub=?
                    ');
                    $getStud->bind_param('ii', $sbj, $active);
                    if ($getStud->execute()) {
                        $getStudRes = $getStud->get_result();
                        while ($s = mysqli_fetch_assoc($getStudRes)) {
                            ?>
                            <li>
                                <div class="col-xs-9">
                                    <label for="dg"><?php print $s['name'] ?></label>
                                </div>
                                <div class="col-xs-3 text-center">
                                    <input type="text" name="dg" id="dg"
                                           class="specialTextbox form-control1 input-sm">
                                    <input type="hidden" name="stId[]" value="<?php print $s['std_id'] ?>">
                                </div>
                            </li>
                            <?php
                        }
                    }
                    ?>
                </ul>

                <div class="col-xs-3 pull-left">
                    <input type="submit" name="submit" id="submit" value="تعديل الدرجات"
                           class="btn-success btn width100per">
                </div>
        </form>

这是用于更新的PHP

if (isset($_GET['sbj'])) {
        $sbj = $_GET['sbj'];
    }

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


        foreach ($_POST['stId'] as $key => $value) {
            $dg = $_POST['dg'][$key];
            $stId = $_POST['stId'][$key];
            echo "$key=$value"."<br/>";
            $update = $db->prepare('UPDATE student_subjects SET subDegree=? WHERE std_id=?');
            $update->bind_param('ii', $sbj, $stId);
            if ($update->execute()) {
                header('Location:?c=10&stdSys=9&sudRes=3&sbj=' . $sbj);
            } else {
                printf("Error : %s\n", $db->error);
            }
        }
    }   

3 个答案:

答案 0 :(得分:0)

我要看到的第一件事是你应该给输入ID,以便它们不会互相覆盖:

<input type="hidden" name="stId[<?= $s['std_id'] ?>]" value="<?= $s['std_id'] ?>">

第二件事是,如果你在foreach循环中执行标题位置,它将永远不会完成

foreach ($_POST['stId'] as $key => $value) {
    ...
    // don't do header location here if you want the loop to be completed
}

// then you can redirect
header('Location:?c=10&stdSys=9&sudRes=3&sbj=' . $sbj);

第三件事是你应该编辑你的dg输入,因为你试图将它作为一个数组来操作但是你没有将它定义为一个数组

<input type="text" name="dg" id="dg[<?= $s['std_id'] ?>" class="specialTextbox form-control1 input-sm">

而不是

<input type="text" name="dg" id="dg" class="specialTextbox form-control1 input-sm">

你没有给我们足够的线索来解决你的问题,因为你没有给出任何错误或解释为什么你认为它不起作用,你被困在哪里等等。

答案 1 :(得分:0)

当您到达header

时,您将在第一行插入后离开执行

答案 2 :(得分:0)

请将数组符号“[]”放在“dg”输入名称属性中。请在下面的脚本中进行更正,现在检查一下它应该可以正常工作。

<input type="text" name="dg[]" id="dg" class="specialTextbox form-control1 input-sm">
<input type="hidden" name="stId[]" value="<?php print $s['std_id'] ?>">

foreach ($_POST['stId'] as $key => $value) {
    $dg = $_POST['dg'][$key];
    $stId = $_POST['stId'][$key];
}

谢谢!