[英]PHP PDO Update query affects all rows when execute
I have a problem on using PDO Update query code.我在使用 PDO 更新查询代码时遇到问题。 When I edit a single record of a person by changing the information and saving it by clicking the save button, it affects all records that should only update a single record.
当我通过更改信息并通过单击保存按钮保存它来编辑一个人的单个记录时,它会影响应该只更新单个记录的所有记录。 All records have the same information now.
所有记录现在都具有相同的信息。 How can I make it update a single record without affecting the others?
如何使其更新单个记录而不影响其他记录? Thank you in advance.
先感谢您。
update.php更新.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>
Posted as a community wiki, I want no rep from this.作为社区维基发布,我不想要任何代表。
age = :t_age;
<<< is an end of statement character. <<< 是语句结束符。 That should be a comma.
那应该是逗号。 That's why it's updating everything.
这就是它更新所有内容的原因。
The semi-colon is actually a valid character and won't throw an error for it.分号实际上是一个有效的字符,不会为它抛出错误。 It will also not update the
date_birth
column neither.它也不会更新
date_birth
列。
Reference:参考:
As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement.
与在 C 或 Perl 中一样,PHP 要求在每条语句的末尾以分号终止指令。 The closing tag of a block of PHP code automatically implies a semicolon;
PHP 代码块的结束标记自动暗示一个分号; you do not need to have a semicolon terminating the last line of a PHP block.
你不需要用分号来结束 PHP 块的最后一行。 The closing tag for the block will include the immediately trailing newline if one is present.
块的结束标记将包括紧随其后的换行符(如果存在)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.