How can we batch modify database data in PHP?
To mass edit database data, you can utilize the MySQLi extension in PHP. Below is a simple example code for batch editing database data:
<?php
// 连接到数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 定义要修改的数据
$ids = array(1, 2, 3);
$newValue = "New Value";
// 构建更新数据的SQL语句
$sql = "UPDATE table_name SET column_name = '$newValue' WHERE id IN (".implode(',', $ids).")";
// 执行SQL语句
if ($conn->query($sql) === TRUE) {
echo "数据更新成功";
} else {
echo "数据更新失败: " . $conn->error;
}
// 关闭数据库连接
$conn->close();
?>
In the example code above, we first connect to the database, then define the data to be modified (in this case, we are modifying data with IDs 1, 2, and 3), then build the SQL statement for updating the data, and finally execute the SQL statement to update the data. Finally, we close the database connection.
Please note that this is just a simple example code, and may need to be modified and improved according to specific circumstances in actual applications.