Correction status:Uncorrected
Teacher's comments:
更新uadate
<?php /** * 预处理的简化版本:以新为例 * 一次新增多条进行介绍 */ //1.连接数据库 require 'mysqli_connect.php'; //2.准备SQL语句 $sql = "UPDATE staff set salary =? WHERE sex=?;"; //3.创建stmt对象 $stmt = mysqli_stmt_init($db); //4.检测SQL语句 if (mysqli_stmt_prepare($stmt, $sql)) { /* 参数绑定 */ mysqli_stmt_bind_param($stmt, "ii", $salary,$sex); $sex= 1; $salary = 8888; /* 执行SQL语句 */ mysqli_stmt_execute($stmt); echo '<br>更新了'.mysqli_stmt_affected_rows($stmt).'条记录,主键是:'.mysqli_stmt_insert_id($stmt); //再次添加新数据,只需要给新变量并执行一下就可以了 // $sex= 0; // $salary = 6666; // // /* 执行SQL语句 */ // mysqli_stmt_execute($stmt); // echo '<br>更新了'.mysqli_stmt_affected_rows($stmt).'条记录,主键是:'.mysqli_stmt_insert_id($stmt); } else { exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt)); } /* 注销stmt对象 */ mysqli_stmt_close($stmt); /* 关闭数据库连接 */ mysqli_close($db);
点击 "运行实例" 按钮查看在线实例
删除 delete
<?php /** * 预处理的简化版本:以新为例 * 一次新增多条进行介绍 */ //1.连接数据库 require 'mysqli_connect.php'; //2.准备SQL语句 $sql = "DELETE FROM staff WHERE salary=?;"; //3.创建stmt对象 $stmt = mysqli_stmt_init($db); //4.检测SQL语句 if (mysqli_stmt_prepare($stmt, $sql)) { /* 参数绑定 */ mysqli_stmt_bind_param($stmt, "i", $salary); $salary = 6000; /* 执行SQL语句 */ mysqli_stmt_execute($stmt); echo '<br>删除了'.mysqli_stmt_affected_rows($stmt).'条记录,主键是:'.mysqli_stmt_insert_id($stmt); //再次添加新数据,只需要给新变量并执行一下就可以了 // $sex= 0; // $salary = 6666; // // /* 执行SQL语句 */ // mysqli_stmt_execute($stmt); // echo '<br>更新了'.mysqli_stmt_affected_rows($stmt).'条记录,主键是:'.mysqli_stmt_insert_id($stmt); } else { exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt)); } /* 注销stmt对象 */ mysqli_stmt_close($stmt); /* 关闭数据库连接 */ mysqli_close($db);
点击 "运行实例" 按钮查看在线实例