簡體   English   中英

PHP PDO 更新查詢在執行時影響所有行

[英]PHP PDO Update query affects all rows when execute

我在使用 PDO 更新查詢代碼時遇到問題。 當我通過更改信息並通過單擊保存按鈕保存它來編輯一個人的單個記錄時,它會影響應該只更新單個記錄的所有記錄。 所有記錄現在都具有相同的信息。 如何使其更新單個記錄而不影響其他記錄? 先感謝您。

更新.php

<?php
include ('includes/connection.php');
$id = isset($_GET['id']) ? $_GET['id']: die('Error: Record ID not found.');

try {
    $query_select = "SELECT id, profile_picture, first_name, last_name, gender, age, date_birth FROM tbl_records WHERE id = ? LIMIT 0,1";
    $query_statement = $db_connection->prepare($query_select);

    $query_statement->bindParam(1, $id);

    $query_statement->execute();

    $row = $query_statement->fetch();

    $profilePicture = $row['profile_picture'];
    $firstName = $row['first_name'];
    $lastName = $row['last_name'];
    $gender = $row['gender'];
    $age = $row['age'];
    $dateBirth = $row['date_birth'];
}

catch(PDOException $e) {
    die('Error 1: '. $e->getMessage());
}

if($_POST) {
    try {
        $query_update = "UPDATE tbl_records SET 
        profile_picture = :t_profile_picture,
        first_name = :t_first_name,
        last_name = :t_last_name,
        gender = :t_gender,
        age = :t_age;
        date_birth = :t_date_birth
        WHERE id = :t_id";

        $query_statement = $db_connection->prepare($query_update);

        $profilePicture = htmlspecialchars(strip_tags($_POST['profile-picture']));
        $firstName = htmlspecialchars(strip_tags($_POST['first-name']));
        $lastName = htmlspecialchars(strip_tags($_POST['last-name']));
        $gender = htmlspecialchars(strip_tags($_POST['gender']));
        $age = htmlspecialchars(strip_tags($_POST['age']));
        $dateBirth  = htmlspecialchars(strip_tags($_POST['date-birth']));

        $query_statement->bindParam(':t_profile_picture', $profilePicture);
        $query_statement->bindParam(':t_first_name', $firstName);
        $query_statement->bindParam(':t_last_name', $lastName);
        $query_statement->bindParam(':t_gender', $gender);
        $query_statement->bindParam(':t_age', $age);
        $query_statement->bindParam(':t_date_birth', $dateBirth);
        $query_statement->bindParam(':t_id', $id);

        if($query_statement->execute()) {
            echo "<div class='alert alert-success' role='start'>Record was updated</div>";
        }

        else {
            echo "<div class='alert alert-danger' role='start'>Unable to update the record.</div>";
        }
            echo var_dump($query_statement->rowCount());
    }

    catch(PDOException $e) {
        die('ERROR 2: ' . $e->getMessage());
    }
}
?>

<html>
<body>
<form action="update.php?id=<?php echo htmlspecialchars($id); ?>" method="post">
    <input type="hidden" name="id" value="<?php echo htmlspecialchars($id, ENT_QUOTES); ?>" />
    <input type="file" name="profile-picture" value="<?php echo htmlspecialchars($profilePicture, ENT_QUOTES); ?>" />

    <label for="first-name">First name:</label> <br />
    <input type="text" name="first-name" value="<?php echo htmlspecialchars($firstName, ENT_QUOTES); ?>" /> <br />

    <label for="last-name">Last name:</label> <br />
    <input type="text" name="last-name" value="<?php echo htmlspecialchars($lastName, ENT_QUOTES); ?>" /> <br />

    <label for="gender">Gender:</label> <br />
    <input type="text" name="gender" value="<?php echo htmlspecialchars($gender); ?>" /> <br />

    <label for="age">Age:</label> <br />
    <input type="text" name="age" value="<?php echo htmlspecialchars($age); ?>" /> <br />

    <label for="date-birth">Date of Birth:</label> <br />
    <input type="date" name="date-birth" value="<?php echo htmlspecialchars($dateBirth); ?>" /> <br />

    <input class="button-style" type="submit" value="SAVE" />
</form>
</body>
</html>

作為社區維基發布,我不想要任何代表。

age = :t_age; <<< 是語句結束符。 那應該是逗號。 這就是它更新所有內容的原因。

分號實際上是一個有效的字符,不會為它拋出錯誤。 它也不會更新date_birth列。

參考:

與在 C 或 Perl 中一樣,PHP 要求在每條語句的末尾以分號終止指令。 PHP 代碼塊的結束標記自動暗示一個分號; 你不需要用分號來結束 PHP 塊的最后一行。 塊的結束標記將包括緊隨其后的換行符(如果存在)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM