【问题标题】:Mysql and PHP Update ProblemsMysql 和 PHP 更新问题
【发布时间】:2017-10-16 19:23:12
【问题描述】:

我正在开发一个简单的用户配置文件,用户可以在其中通过 Mysql 更新一些数据。但它不会更新任何东西。为什么?

<?php

if (isset($_GET['pageID'])) {

    $id = $_GET['pageID'];

    include("connector.php");
    $stmt = $conn->prepare("    SELECT 
                            cl.clientID, cl.firstname, cl.lastname, cl.mobilephone, cl.phone, cl.client_ID,
                            ad.street, ad.city, ad.postalcode,
                            wo.work_ID,
                            ha.hardware1, ha.serialnumber, ha.hardware2, ha.serialnumber2
                            FROM client cl 
                            JOIN adress ad ON cl.clientID = ad.clientID 
                            LEFT JOIN work wo ON cl.clientID = wo.clientID
                            LEFT JOIN hardware ha ON ad.adressID = ha.adressID
                            WHERE cl.clientID = '$id'");
    $stmt->execute();
}
?>
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) : ?>

    <div class="container">

    <div class="col-sm-12 col-md-12 col-xl-12">
        <div class="col-sm-3 col-md-3 col-xl-3 profileNames">
            <img src="Userfotos/jedi.jpg" class="img-responsive"><br>
            <button type="submit" name="submit" class="btn btn-info">edit Foto</button>

        </div>
        <div class="col-sm-3 col-md-3 col-xl-3 profileNames">
            <p><h4>Lastname:</h4><?php echo $row['lastname']; ?></p>
            <p><h4>Firstname:</h4> <?php echo $row['firstname']; ?></p>
            <p><h4>Street</h4> <?php echo $row['street']; ?></p>
            <p><h4>City</h4> <?php echo $row['city']; ?></p>
            <p><h4>Postalcode</h4> <?php echo $row['postalcode']; ?></p>

            <button type="button" class="btn btn-info" data-toggle="modal" data-target="#editPrivateData">Edit data
            </button>

            <div id="editPrivateData" class="modal fade" role="dialog">
                <div class="modal-dialog">

                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Edit Personal data</h4>
                        </div>
                        <div class="modal-body">
                            <form action="userProfile.php?pageID=1" method="post">
                                <div class="form-group">
                                    <label for="_firstname">Firstname</label>
                                    <input type="text" class="form-control" id="_firstname" name="firstname"
                                           value="<?php echo $row['firstname']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="_lastname">Lastname</label>
                                    <input type="text" class="form-control" id="_lastname" name="lastname"
                                           value="<?php echo $row['lastname']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="_street">Street</label>
                                    <input type="text" class="form-control" id="_street" name="street"
                                           value="<?php echo $row['street']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="_city">City</label>
                                    <input type="text" class="form-control" id="_city" name="city"
                                           value="<?php echo $row['city']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="_postalcode">Postalcode</label>
                                    <input type="text" class="form-control" id="_postalcode" name="postalcode"
                                           value="<?php echo $row['postalcode']; ?>">
                                </div>
                                <button type="submit" name="submitPrivate" class="btn btn-info">Save</button>
                                <?php

                                if (isset($_POST['submitHardware'])) {
                                    try {
                                        $id = $_GET['pageID'];
                                        $stmt = $conn->prepare("UPDATE clients.client SET
                                              firstname = :firstname,
                                              lastname = :lastname,
                                              mobilephone = :mobilephone,
                                              phone = :phone
                                              WHERE clientID = $id");

                                        $stmt->bindParam(':firstname', $firstname);
                                        $stmt->bindParam(':lastname', $lastname);
                                        $stmt->bindParam(':mobilephone', $mobilephone);
                                        $stmt->bindParam(':phone', $phone);

                                        $stmt->execute([':firstname' => $_POST['firstname'],
                                            ':lastname' => $_POST['lastname'],
                                            ':mobilephone' => $_POST['mobilephone'],
                                            ':phone' => $_POST['phone']
                                        ]);
                                    } catch (PDOException $e) {
                                        echo $e->getMessage() . ", ";
                                        die("try to fix your inputs . If this doesn`t work, please contact your Admin.");
                                    }

                                }
                                ?>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-3 col-md-3 col-xl-3 profileNames">
            <p><h4>Hardware1:</h4><?php echo $row['hardware1']; ?></p>
            <p><h4>Serialnumber1:</h4><?php echo $row['serialnumber']; ?></p>
            <p><h4>Hardware2:</h4><?php echo $row['hardware2']; ?></p>
            <p><h4>Serialnumber2:</h4><?php echo $row['serialnumber2']; ?></p>
            <button type="button" class="btn btn-info" data-toggle="modal" data-target="#editHardware">Edit data
            </button>

            <!-- Modal -->
            <div id="editHardware" class="modal fade" role="dialog">
                <div class="modal-dialog">

                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Edit hardware data</h4>
                        </div>
                        <div class="modal-body">
                            <form action="userProfile.php?pageID=3" method="post">
                                <div class="form-group">
                                    <label for="_hardware1">Hardware1</label>
                                    <input type="text" class="form-control" id="_hardware" name="hardware1"
                                           placeholder="<?php echo $row['hardware1']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="serialnumber">Serialnumber1</label>
                                    <input type="text" class="form-control" id="_serialnumber" name="serialnumber"
                                           placeholder="<?php echo $row['serialnumber']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="_hardware2">Hardware2</label>
                                    <input type="text" class="form-control" id="_hardware2" name="hardware2"
                                           placeholder="<?php echo $row['hardware2']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="_serialnumber2">Serialnumber2</label>
                                    <input type="text" class="form-control" id="_serialnumber2" name="serialnumber2"
                                           placeholder="<?php echo $row['serialnumber2']; ?>">
                                </div>
                                <button type="submit" name="submitHardware" class="btn btn-info">Save</button>
                                <?php

                                                                if (isset($_POST['submitHardware'])) {
                                                                    require("connector.php");
                                                                    try {
                                                                        $stmt = $conn->prepare("UPDATE clients.hardware SET
                                                                                                              hardware1 = :hardware1,
                                                                                                              serialnumber = :serialnumber,
                                                                                                              hardware2 = :hardware2,
                                                                                                              serialnumber2 = :serialnumber2,
                                                                                                              WHERE hardware.clientID = '$id'");

                                                                        $stmt->bindParam(':firstname', $firstname);
                                                                        $stmt->bindParam(':lastname', $lastname);
                                                                        $stmt->bindParam(':mobilephone', $mobilephone);
                                                                        $stmt->bindParam(':phone', $phone);
                                                                        $stmt->bindParam(':client_ID', $client_ID);

                                                                        $stmt->execute([':hardware1' => $_POST['hardware1'],
                                                                            ':serialnumber' => $_POST['serialnumber'],
                                                                            ':hardware2' => $_POST['hardware2'],
                                                                            ':serialnumber2' => $_POST['seerialnumber2']
                                                                        ]);

                                                                    } catch (PDOException $e) {

                                                                        echo $e->getMessage() . ", ";
                                                                        die("try to fix your inputs . If this doesn`t work, please contact your Admin.");
                                                                    }
                                                                }
                                                                 ?>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-3 col-md-3 col-xl-3 profileNames">
            <p><h4>ClientID:</h4><?php echo $row['client_ID']; ?></p>
            <p><h4>WorkID:</h4><?php echo $row['work_ID']; ?></p>
            <button type="submit" name="submit" class="btn btn-info">Edit data</button>
        </div>
    </div>
<?php endwhile; ?>

如您所见,有一个while-loop 来获取数据并将它们显示到某些字段中。在此循环之间,我创建了两个Bootstrap-Modals,用户可以在其中编辑某些内容。如果用户单击“保存”按钮,则不会发生任何事情。我真的不明白问题出在哪里。

还有一点:用户配置文件是使用 clientID(pageID) 动态生成的。我如何临时保存这个pageID,这样我可以在表单操作method ="POST"后返回当前页面@

clientID 是数据库中clients.client 表的主键。 client_ID 是用户必须自己设置的ID,他可以设置任何他想要的。 PageID 链接到 clientID 用于生成动态页面。这意味着:在另一个页面上有一个表格,其中显示了有关所有用户的一些基本信息。如果登录的用户点击表中的用户,则会动态生成用户配置文件。

【问题讨论】:

  • 从 url 中移除 pageID=1
  • 不要将更新逻辑放入循环人
  • 如果你点击保存,你的页面会刷新吗?
  • 别抱歉,只是纠正错误,尤其是缺少大写字母。
  • @Christoph,您可以将 pageID 存储在隐藏字段中,并在表单提交时发布

标签: php html mysql pdo


【解决方案1】:

你的代码真的很奇怪。请告诉我们页面 ID 和客户端 ID 是什么。它们显然不一样。但是你就这样使用它们。我将您的代码简化为一种模式,并为客户端和页面 ID 值使用了隐藏字段。

<?php


// TODO: filter all input
if (isset($_POST['submitHardware']) && $_POST['pageID'] == 3) {
    try {
        include("connector.php");

        $client_id = $_GET['clientID'];
        $stmt = $conn->prepare("UPDATE clients.client SET
              firstname = :firstname,
              lastname = :lastname,
              mobilephone = :mobilephone,
              phone = :phone
              WHERE clientID = $client_id");

        $stmt->bindParam(':firstname', $firstname);
        $stmt->bindParam(':lastname', $lastname);
        $stmt->bindParam(':mobilephone', $mobilephone);
        $stmt->bindParam(':phone', $phone);

        $stmt->execute([':firstname' => $_POST['firstname'],
            ':lastname' => $_POST['lastname'],
            ':mobilephone' => $_POST['mobilephone'],
            ':phone' => $_POST['phone']
        ]);
    } catch (PDOException $e) {
        echo $e->getMessage() . ", ";
        die("try to fix your inputs . If this doesn`t work, please contact your Admin.");
    }

}

if (isset($_GET['clientID'])) {

    $client_id = $_GET['clientID']; // TODO: filter

    include("connector.php");

    $stmt = $conn->prepare("    SELECT 
                            cl.clientID, cl.firstname, cl.lastname, cl.mobilephone, cl.phone, cl.client_ID,
                            ad.street, ad.city, ad.postalcode,
                            wo.work_ID,
                            ha.hardware1, ha.serialnumber, ha.hardware2, ha.serialnumber2
                            FROM client cl 
                            JOIN adress ad ON cl.clientID = ad.clientID 
                            LEFT JOIN work wo ON cl.clientID = wo.clientID
                            LEFT JOIN hardware ha ON ad.adressID = ha.adressID
                            WHERE cl.clientID = '$client_id'");
    $stmt->execute();
}
?>
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) : ?>

    <div class="container">

    <div class="col-sm-12 col-md-12 col-xl-12">
        <div class="col-sm-3 col-md-3 col-xl-3 profileNames">
            <img src="Userfotos/jedi.jpg" class="img-responsive"><br>
            <button type="submit" name="submit" class="btn btn-info">edit Foto</button>

        </div>
        <div class="col-sm-3 col-md-3 col-xl-3 profileNames">
            <p><h4>Hardware1:</h4><?php echo $row['hardware1']; ?></p>
            <p><h4>Serialnumber1:</h4><?php echo $row['serialnumber']; ?></p>
            <p><h4>Hardware2:</h4><?php echo $row['hardware2']; ?></p>
            <p><h4>Serialnumber2:</h4><?php echo $row['serialnumber2']; ?></p>
            <button type="button" class="btn btn-info" data-toggle="modal" data-target="#editHardware">Edit data
            </button>

            <!-- Modal -->
            <div id="editHardware" class="modal fade" role="dialog">
                <div class="modal-dialog">

                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Edit hardware data</h4>
                        </div>
                        <div class="modal-body">
                            <form action="userProfile.php" method="post">
                                <input type="hidden" name="pageID" value="3">
                                <input type="hidden" name="clientID" value="<?php echo $client_id; ?>">
                                <div class="form-group">
                                    <label for="_hardware1">Hardware1</label>
                                    <input type="text" class="form-control" id="_hardware" name="hardware1"
                                           placeholder="<?php echo $row['hardware1']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="serialnumber">Serialnumber1</label>
                                    <input type="text" class="form-control" id="_serialnumber" name="serialnumber"
                                           placeholder="<?php echo $row['serialnumber']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="_hardware2">Hardware2</label>
                                    <input type="text" class="form-control" id="_hardware2" name="hardware2"
                                           placeholder="<?php echo $row['hardware2']; ?>">
                                </div>
                                <div class="form-group">
                                    <label for="_serialnumber2">Serialnumber2</label>
                                    <input type="text" class="form-control" id="_serialnumber2" name="serialnumber2"
                                           placeholder="<?php echo $row['serialnumber2']; ?>">
                                </div>
                                <button type="submit" name="submitHardware" class="btn btn-info">Save</button>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-info" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php endwhile; ?>

【讨论】:

    猜你喜欢
    • 2014-03-20
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多