PHP MySQL UPDATE 查询
在本教程中,你将学习如何使用 PHP 更新 MySQL 表中的记录。
更新数据库表数据
UPDATE
语句用于更改或修改数据库表中的现有记录。此语句通常与 WHERE
子句结合使用,以仅将更改应用于符合特定条件的记录。
UPDATE
语句的基本语法如下:
UPDATE table_name SET column1=value, column2=value2,... WHERE column_name=some_value
让我们使用 UPDATE
语句和 WHERE
子句进行 SQL 查询,之后我们将执行此查询,方法是将其传递给 PHP mysqli_query()
函数以更新表记录。考虑 demo 数据库中的 persons 表 :
+----+------------+-----------+----------------------+
| id | first_name | last_name | email |
+----+------------+-----------+----------------------+
| 1 | Peter | Parker | peterparker@mail.com |
| 2 | John | Rambo | johnrambo@mail.com |
| 3 | Clark | Kent | clarkkent@mail.com |
| 4 | John | Carter | johncarter@mail.com |
| 5 | Harry | Potter | harrypotter@mail.com |
+----+------------+-----------+----------------------+
以下示例中的 PHP 代码将更新 persons
表中 id 等于 1 的人员的电子邮件地址。
面向过程方式
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "demo");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt update query execution
$sql = "UPDATE persons SET email='peterparker_new@mail.com' WHERE id=1";
if(mysqli_query($link, $sql)){
echo "Records were updated successfully.";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
?>
面向对象方式
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$mysqli = new mysqli("localhost", "root", "", "demo");
// Check connection
if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}
// Attempt update query execution
$sql = "UPDATE persons SET email='peterparker_new@mail.com' WHERE id=1";
if($mysqli->query($sql) === true){
echo "Records were updated successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
// Close connection
$mysqli->close();
?>
PDO 方式
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
try{
$pdo = new PDO("mysql:host=localhost;dbname=demo", "root", "");
// Set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
die("ERROR: Could not connect. " . $e->getMessage());
}
// Attempt update query execution
try{
$sql = "UPDATE persons SET email='peterparker_new@mail.com' WHERE id=1";
$pdo->exec($sql);
echo "Records were updated successfully.";
} catch(PDOException $e){
die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
// Close connection
unset($pdo);
?>
更新后,persons 表将如下所示:
+----+------------+-----------+--------------------------+
| id | first_name | last_name | email |
+----+------------+-----------+--------------------------+
| 1 | Peter | Parker | peterparker_new@mail.com |
| 2 | John | Rambo | johnrambo@mail.com |
| 3 | Clark | Kent | clarkkent@mail.com |
| 4 | John | Carter | johncarter@mail.com |
| 5 | Harry | Potter | harrypotter@mail.com |
+----+------------+-----------+--------------------------+
警告: 在 UPDATE
语句中的 WHERE
子句指定一个或多个记录应更新。如果省略该 WHERE
子句,则将更新所有记录。