Correction status:Uncorrected
Teacher's comments:
预处理流程:
连接数据库
准备sql语句
创建stmt对象(mysqli_stmt_init(DB))
检测sql语句(mysqli_stmt_prepare(STMT,SQL))
绑定预处理元素(mysqli_stmt_bind_param(STMT,TYPE,OBJ))
提交预处理(mysqli_stmt_execute(STMT))
注销对象(mysqli_stmt_close(STMT))
关闭连接(mysqli_close(DB))
UPDATE操作
<?php header("Content-type: text/html; charset=utf-8"); // 1.连接数据库 require "connect.php"; // 2.准备sql语句 $sql="UPDATE user SET name=?,age=?,salary=? WHERE id=4" ; //3.创建stmt对象 $stmt=mysqli_stmt_init($conn); //3.检测sql语句 if(mysqli_stmt_prepare($stmt,$sql)){ //绑定预处理元素 mysqli_stmt_bind_param($stmt,'sii',$name,$age,$salary); $name="渣渣辉"; $age=23; $salary=1600; //执行预处理 mysqli_stmt_execute($stmt); echo "影响了:".mysqli_stmt_affected_rows($stmt)."条记录"; }else{ // 错误信息打印 exit(mysqli_stmt_errno($stmt).":".mysqli_stmt_error($stmt)); } //注销stmt对象 mysqli_stmt_close($stmt); //关闭连接t mysqli_close($conn);
点击 "运行实例" 按钮查看在线实例
DELETE操作:流程基本一致
<?php header("Content-type: text/html; charset=utf-8"); //创建数据库链接 require "connect.php"; //创建sql语句 $sql="DELETE FROM user WHERE name=?;"; //创建stmt对象 $stmt=mysqli_stmt_init($conn); //检查sql语句 if(mysqli_stmt_prepare($stmt,$sql)){ // 绑定sql元素 mysqli_stmt_bind_param($stmt,'s',$name); $name='渣渣辉'; // 执行预处理 mysqli_stmt_execute($stmt); // 返回处理结果 echo "删除了:".mysqli_stmt_affected_rows($stmt)."行"; }else{ exit (mysqli_stmt_errno($stmt).":".mysqli_stmt_error($stmt)); } mysqli_stmt_close($stmt); mysqli_close($conn);
点击 "运行实例" 按钮查看在线实例