Correction status:Uncorrected
Teacher's comments:
预处理实现更新数据
$sql = "UPDATE staff SET name=?,salary=? WHERE staff_id=?;"; $stmt = mysqli_stmt_init($db); if (mysqli_stmt_prepare($stmt,$sql)){ mysqli_stmt_bind_param($stmt,'sii',$name,$salary,$staff_id); $name = 'xiaolongnn';$salary = 5600;$staff_id=15; if(mysqli_stmt_execute($stmt)){ if (mysqli_stmt_affected_rows($stmt)>0){ echo '更新成功,记录:' . mysqli_stmt_affected_rows($stmt); }else{ echo '没有更新记录'; } }else{ exit(mysqli_stmt_errno($stmt) . ':' . mysqli_stmt_error($stmt)); } }else{ exit(mysqli_stmt_errno($stmt) . ':' . mysqli_stmt_error($stmt)); } mysqli_stmt_close($stmt); mysqli_close($db);
预处理实现删除数据
require 'mysqli-connect.php'; $sql = "DELETE FROM staff WHERE staff_id=?"; $stmt = mysqli_stmt_init($db); if (mysqli_stmt_prepare($stmt,$sql)){ mysqli_stmt_bind_param($stmt,'i',$staff_id); $staff_id = 19; if(mysqli_stmt_execute($stmt)){ if (mysqli_stmt_affected_rows($stmt)>0){ echo '删除成功:' . mysqli_stmt_affected_rows($stmt); }else{ echo '删除失败'; } }else{ exit(mysqli_stmt_errno($stmt) . ':' . mysqli_stmt_error($stmt)); } }else{ exit(mysqli_stmt_errno($stmt) . ':' . mysqli_stmt_error($stmt)); } mysqli_stmt_close($stmt); mysqli_close($db);
总结
预处理操作:使用mysqli_stmt_init()创建stmt对象
mysqli_stmt_prepare检测stmt对象
mysqli_stmt_bind_param()邦定stmt对象
执行stmt对象mysqli_stmt_excute()
mysqli_stmt_close($stmt);关闭预处理对象
mysqli_close($db);不要忘记关闭数据库