Mysql和PHP更新问题

时间:2017-10-16 09:45:51

标签: php html mysql pdo

我正在开发一个简单的Userprofile,用户可以通过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,用户可以在其中编辑内容。如果用户点击“保存”按钮,则没有任何反应。我真的不明白问题出在哪里。

另一个小问题:userprofile是使用clientID(pageID)动态生成的。如何临时保存此pageID,以便我可以在表单操作method ="POST"

之后返回当前页面

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

1 个答案:

答案 0 :(得分:0)

你的代码真的很奇怪。请告诉我们页面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; ?>