繁体   English   中英

php 只更新第一个数据库条目,而不管我尝试更新哪个条目

[英]php only updating first database entry regardless of what entry i try to update

所以我正在为学校做一个项目,我正在尝试更新我数据库中某些行的列中的某些项目,但是,无论我尝试更新什么条目,只有第一个条目才会进行预期的更改。

这是我的更新功能:

if (isset($_POST['update']) && isset($_POST['serviceNum']))
{
    $progress = get_post($conn, 'progress');
    $serviceNum = get_post($conn, 'serviceNum');
    $query = "UPDATE classics SET progress='$progress' WHERE serviceNum='$serviceNum'";
    $result = $conn->query($query);
    if (!$result) echo "UPDATE failed: $query<br>" .
        $conn->error . "<br><br>";
}

这是我的更新表格:

<form action="admin.php" method="post">
                    <br>

   <select name="progress">
        <option value=" "> </option>
        <option value="In Progress">In Progress</option>
        <option value="Completed">Completed</option>
    </select>

    <input type="hidden" name="update" value="progress">
    <input type="hidden" name="serviceNum" value="$row[0]">

    <br><br>
    <input type="submit" value="UPDATE">
    <br><br>
</form>

$row[0]是包含主键 serviceNum 的行。 我将表单嵌套在表中,当我在本地主机上运行它并查看源代码时,它显示每个条目都具有该行数据的正确 serviceNum。 但是,无论我在哪个条目上使用更新按钮,它都只会更新数据库表的第一个条目。

编辑:

for ($j = 0; $j < $rows; ++$j)
{
    $result->data_seek($j);
    $row = $result->fetch_array(MYSQLI_NUM);

    echo "<tr>";

    echo <<<_END
    <td>$row[0]</td>
    <td>$row[1]</td>
    <td>$row[2]</td>
    <td>$row[3]</td>
    <td>$row[4]</td>
    <td>

        <!-- Trigger Update Modal -->
        <center>
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#UpdateModal"> <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span> </button>
        </center>

        <!-- Update Modal -->

        <div class="modal fade" id="UpdateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog"role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title" id="myModalLabel">Update Record</h4>
                    </div>
                    <div class="modal-boday">
                        <center>
                        <form action="admin.php" method="post">
                            <br>

                            <select name="progress">
                                <option value=" "> </option>
                                <option value="In Progress">In Progress</option>
                                <option value="Completed">Completed</option>
                            </select>

                            <input type="hidden" name="update" value="yes">
                            <input type="hidden" name="serviceNum" value="$row[0]">

                            <br><br>
                            <input type="submit" value="UPDATE">

编辑 2:模态

                            <center>
                                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#UpdateModal">
                                    <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span>
                                </button>
                            </center>

                            <!-- Update Modal -->

                            <div class="modal fade" id="UpdateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                            </button>
                                            <h4 class="modal-title" id="myModalLabel">Update Record</h4>
                                        </div>
                                        <div class="modal-boday">
                                        <center>
                                            <form action="admin.php" method="post">
                                                <br>
                                                <select name="progress">
                                                    <option value=" "> </option>
                                                    <option value="In Progress">In Progress</option>
                                                    <option value="Completed">Completed</option>
                                                </select>

                                                <input type="hidden" name="update" value="status">
                                                <input type="hidden" name="ticketnum" value="$row[0]">
                                                <br><br>
                                                <input type="submit" value="UPDATE">
                                                <br><br>
                                            </form>
                                        </center>
                                    </div>
                                </div>
                            </div>

                            <!-- End Update Modal -->

问题是您在表的每一行都使用相同的id="update-modal" ID 应该是唯一的,查找 ID 总是返回第一个。 所以data-target="#update-modal"总是针对第一行的模态,不管你点击哪一个。

将项目 ID 添加到update-modal ID 中。

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#UpdateModal{$row[0]}">

<div class="modal fade" id="UpdateModal{$row[0]}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM