PHP MySQL Update: Step-by-Step Guide
The usual method to update table data in PHP is to execute an UPDATE operation using SQL statements. Here is a simple example:
<?php
// 连接到数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接是否成功
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 更新表数据
$sql = "UPDATE users SET name='John' WHERE id=1";
if ($conn->query($sql) === TRUE) {
echo "数据更新成功";
} else {
echo "数据更新失败: " . $conn->error;
}
// 关闭连接
$conn->close();
?>
In the example above, we used the UPDATE statement to update the name field of the record with id 1 in the table named “users” to “John”. You can modify the SQL statement to update different tables and fields as needed.