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

标签 php mysql pdo

我在使用 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>

最佳答案

作为社区 Wiki 发布,我不希望有任何代表。

age = :t_age; <<< 是语句结束字符。那应该是一个逗号。这就是它更新所有内容的原因。

分号实际上是一个有效字符,不会引发错误。 它也不会更新 date_birth 列。

引用:

As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present.

关于PHP PDO 更新查询在执行时影响所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39690144/

相关文章:

php - 硬编码的用户/密码适用于 MySQL 连接,但文件中的字符串不适用于

php - 如何将 BLOB 与 JSON 和 PHP 一起使用?

python - 将csv导入mysql数据库

php - PDO 事务不捕获异常

php - Laravel 5 POST 路由到索引而不是存储

php - 在 Codeigniter 中使用 AJAX 显示图像列表

php - 如何在 MySQL 中获取数据库列表和有权访问它的用户

mysql - Mariadb 加载,但几分钟后崩溃。服务器从 "snapshot"恢复后发生

php - PDO 中 fetchColumn() 的非对象错误

php - 在 for 或 foreach 循环中执行此查询是不好的做法吗?