dtyqeoc70733 2017-05-07 20:44
浏览 7
已采纳

跟踪哪些列数据应该进入

Problem

So, I have this program that allows the user to edit student info in the db. When the submit button is clicked on the info goes into an array, so I can easily loop through the info and put the info into the db. But, the problem is that I don't know how to figure out which column the info goes into.

My current method is by using a variable to keep track of the column that the info should go into, but that doesn't work.

I'm my html, I'm putting the student info into an input, so the user can edit the info.

Students Table

studentID | firstname | lastname | teacherID
1         | Bob       | Roberts  | 2
2         | Rick      | Phil     | 1

PHP Form

<form action="server/edit/students.php" method="post">
                <table>
                    <tr>
                        <th>Student ID</th>
                        <th>Firstname</th>
                        <th>Lastname</th>
                        <th>Teacher ID</th>
                    </tr>
                    <?php
                    // get student info
                    $getStudent = $link->prepare("SELECT * FROM students");
                    $getStudent->execute();
                    $getStudent = $getStudent->fetchAll(PDO::FETCH_ASSOC);
                    $value = 0; // counts rows
                    // loop through each student
                    foreach ($getStudent as $student) {
                        $studentID = $student['studentID'];
                        $firstname = $student['firstname'];
                        $lastname = $student['lastname'];
                        $teacherID = $student['teacherID'];

                     ?>

                        <tr>
                            <td>
                                <?php echo $studentID ?>
                            </td>
                            <td>
                                <input type='text' name='student[<?php echo $studentID ?>][firstname]' value='<?php echo $firstname ?>' />
                            </td>
                            <td>
                                <input type='text' name='student[<?php echo $studentID ?>][lastname]' value='<?php echo $lastname ?>' />
                            </td>
                            <td>
                                <input type='text' name='student[<?php echo $studentID ?>][teacherID]' value='<?php echo $teacherID ?>' />
                            </td>
                        </tr>

                        <?php
                            // add to row
                            $value += 1;

                    }
                    ?>
                </table>
                <button type="submit" name="update">Update</button>
            </form>

PHP Code That Process's Form Info

$counter = 0; // keeps track of number of columns

// get data and loop through it
foreach ($_POST['student'] as $id => $data) {
    foreach ($data as $d) {
        if($counter > 1) {
            $counter = 0;

            update($counter, $link, $id, $d);
        } else {
            update($counter, $link, $id, $d);
        }
    }
}

function update($counter, $link, $id, $d) {

    if($counter == 0) {
        $update = $link->prepare("UPDATE students SET firstname = :firstname WHERE studentID = :id");
        $update->execute(array(
            "firstname" => $d,
            "id" => $id
        ));

        echo $counter . "<br>";

        $counter++;
    } else if($counter == 1) {
        $update = $link->prepare("UPDATE students SET lastname = :lastname WHERE studentID = :id");
        $update->execute(array(
            "lastname" => $d,
            "id" => $id
        ));

        echo $counter . "<br>";

        $counter++;
    }

}
  • 写回答

1条回答 默认 最新

  • dongmeijian1716 2017-05-07 21:22
    关注

    The issue is because your $counter variable is not being passed as a reference. Try the following:

    function update(&$counter, $link, $id, $d) {
    

    Before, the variable being passed would always equate to 0. When you increment it, the change isn't saved. When you pass by reference using the & sign, it tells PHP to update the variable you originally sent and not a new copy.

    Edit: Thanks Bob!

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

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序