批改状态:合格
老师批语:
<?php
// 连接数据库
require 'mysqli_connect.php';
// 准备 SQL 语句
$sql = "UPDATE staff SET salary = ? WHERE staff_id = ?;";
// 创建 STMT 预处理对象
$stmt = $mysqli->smtmt_init();
// 检测预处理 SQL 语句
if ($stmt->prepare($sql)) {
// 绑定参数到预处理 SQL 语句
$stmt->bind_param('ii', $salary, $staff_id);
// 设置参数
$salary = 7555;
$staff_id = 3;
// 执行预处理语句
$stmt->execute();
if ($stmt->affected_rows > 0) {
echo '<br>成功更新'.$stmt->affected_rows.'条记录';
} else {
echo '<br>没有更新记录';
}
// 注销 stmt 对象
$stmt->close();
} else {
exit($stmt->error . ':' . $stmt->error);
}
// 关闭数据库连接
$mysqli->close();点击 "运行实例" 按钮查看在线实例
<?php
// 连接数据库
try {
$connect = new PDO('mysql:dbname=php','gakkispy', 'password');
} catch (PDOException $err) {
exit($err->getMessage());
}
// 准备 SQL 语句
$sql = "DELETE FROM staff WHERE staff_id=:staff_id;";
// 创建 stmt 预处理对象
if ($stmt = $connect->prepare($sql)) {
// 绑定参数 stmt 对象
// 参数
$param = ['staff_id'=>9];
// 绑定参数到预处理 SQL 语句
if ($stmt -> execute($param)) {
// rowCount() 返回执行的数量
if ($stmt->rowCount()>0) {
echo '成功删除了'.$stmt->rowCount().'条记录';
} else {
echo '没有记录被删除';
}
} else {
// 失败
print_r($stmt->errorInfo());
}
} else {
// $stmt s语句对象创建失败
print_r($connect->errorInfo());
exit();
}点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号