PHP PDO Update查询在执行时影响所有行


PHP PDO Update query affects all rows when execute

我有一个使用PDO更新查询代码的问题。当我通过更改信息并通过单击保存按钮保存它来编辑某人的单个记录时,它会影响应该只更新单个记录的所有记录。现在所有的记录都有相同的信息。如何使它更新一条记录而不影响其他记录?提前谢谢你。

update.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>

作为一个社区wiki发布,我不想因此受到批评。

age = :t_age; <<是语句结束字符。应该用逗号。这就是它更新一切的原因。

分号实际上是一个有效的字符,不会因此抛出错误。它也不会更新date_birth列。

参考:

  • http://php.net/manual/en/language.basic-syntax.instruction-separation.php

与C或Perl一样,PHP要求指令以结束,在每个语句的末尾以分号结束。PHP代码块的结束标签会自动包含分号;不需要在PHP代码块的最后一行用分号结束。如果出现换行符,块的结束标记将包括紧跟其后的换行符。