用预处理方法完成添加操作
流程
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>用预处理方法完成添加操作</title> <style type="text/css"> .box {width:90%;line-height:1.2em;border:1px dotted #d6d6d6;padding-left:15px;background-color:#f5f5f5;margin:10px auto;font-family:微软雅黑;} .box1 {width:90%;line-height:1.5em;border:1px dotted #d6d6d6;background-color:#f2edd7;font-family:微软雅黑;} .box p {margin-left:20px;font-size:14px;} hr {height:1px;width:95%;border:none;border-top:1px dashed #666;} </style> </head> <body> <?php echo '<div class="box">'; echo '<h4>用预处理方法完成添加操作</h4>'; echo '<div class="box1">'; echo '步骤: * 1.连接数据库 * 2.准备SQL语句 * 3.创建stmt对象 * 4.检测SQL语句 (参数绑定 执行SQL语句 ) * 5.注销stmt对象 * 6.关闭数据库连接'; echo '</div>'; echo "<br>"; echo '<div class="box1"> <p>1. 创建连接参数</p>'; echo "<hr>"; echo "<p>require 'mysqli_connect.php';</p>"; echo "<br>"; echo '<p>2. 准备SQL语句</p>'; echo "<hr>"; echo "<p>\$sql = \"INSERT IGNORE staff SET name=?;\";</p>"; echo "<br>"; echo '<p>3. 创建stmt对象</p>'; echo "<hr>"; echo "<p>\$stmt = mysqli_stmt_init(\$db);</p>"; echo "<br>"; echo '<p>4. 检测SQL语句 (参数绑定 执行SQL语句 )</p>'; echo "<hr>"; echo "<p>if (mysqli_stmt_prepare(\$stmt, $sql)) {</p>"; /* 参数绑定 */ echo "<p> mysqli_stmt_bind_param(\$stmt, \"i\", \$name);</p>"; echo "<p> \$name = '宋小宝';</p>"; /* 执行SQL语句 */ echo "<p> mysqli_stmt_execute(\$stmt);</p>"; echo "<p> echo '新增了'.mysqli_stmt_affected_rows(\$stmt).'条记录,主键是:'.mysqli_stmt_insert_id(\$stmt);</p>"; echo "<p>} else {</p>"; echo "<p> exit(mysqli_stmt_errno(\$stmt).':'.mysqli_stmt_error(\$stmt));</p>"; echo "<p>}</p>"; echo "<br>"; echo '<p>5. 注销stmt对象</p>'; echo "<hr>"; echo "<p>mysqli_stmt_close(\$stmt);</p>"; echo "<br>"; echo '<p>6.关闭数据库连接</p>'; echo "<hr>"; echo "<p>mysqli_close(\$db);</p>"; echo "<br>"; echo '</div>'; echo "<br>"; ?> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
php代码
实例
<?php //1.连接数据库 require 'mysqli_connect.php'; //2.准备SQL语句 $sql = "INSERT IGNORE staff SET name=?;"; //3.创建stmt对象 $stmt = mysqli_stmt_init($db); //4.检测SQL语句 if (mysqli_stmt_prepare($stmt, $sql)) { /* 参数绑定 */ mysqli_stmt_bind_param($stmt, "s", $name); $name = '宋小宝'; /* 执行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);
运行实例 »
点击 "运行实例" 按钮查看在线实例
用预处理方法完成删除操作
流程
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>用预处理方法完成删除操作</title> <style type="text/css"> .box {width:90%;line-height:1.2em;border:1px dotted #d6d6d6;padding-left:15px;background-color:#f5f5f5;margin:10px auto;font-family:微软雅黑;} .box1 {width:90%;line-height:1.5em;border:1px dotted #d6d6d6;background-color:#f2edd7;font-family:微软雅黑;} .box p {margin-left:20px;font-size:14px;} hr {height:1px;width:95%;border:none;border-top:1px dashed #666;} </style> </head> <body> <?php echo '<div class="box">'; echo '<h4>用预处理方法完成删除操作</h4>'; echo '<div class="box1">'; echo '步骤: * 1.连接数据库 * 2.准备SQL语句 * 3.创建stmt对象 * 4.检测SQL语句 (参数绑定 执行SQL语句 ) * 5.注销stmt对象 * 6.关闭数据库连接'; echo '</div>'; echo "<br>"; echo '<div class="box1"> <p>1. 创建连接参数</p>'; echo "<hr>"; echo "<p>require 'mysqli_connect.php';</p>"; echo "<br>"; echo '<p>2. 准备SQL语句</p>'; echo "<hr>"; echo "<p>\$sql = \"DELETE FROM staff WHERE staff_id=?;\";</p>"; echo "<br>"; echo '<p>3. 创建stmt对象</p>'; echo "<hr>"; echo "<p>\$stmt = mysqli_stmt_init(\$db);</p>"; echo "<br>"; echo '<p>4. 检测SQL语句 (参数绑定 执行SQL语句 )</p>'; echo "<hr>"; echo "<p>if (mysqli_stmt_prepare(\$stmt, $sql)) {</p>"; /* 参数绑定 */ echo "<p> mysqli_stmt_bind_param(\$stmt, \"i\", \$id);</p>"; echo "<p> \$id = '9';</p>"; /* 执行SQL语句 */ echo "<p> mysqli_stmt_execute(\$stmt);</p>"; echo "<p> echo '成功删除了'.mysqli_stmt_affected_rows(\$stmt).'条记录';</p>"; echo "<p>} else {</p>"; echo "<p> exit(mysqli_stmt_errno(\$stmt).':'.mysqli_stmt_error(\$stmt));</p>"; echo "<p>}</p>"; echo "<br>"; echo '<p>5. 注销stmt对象</p>'; echo "<hr>"; echo "<p>mysqli_stmt_close(\$stmt);</p>"; echo "<br>"; echo '<p>6.关闭数据库连接</p>'; echo "<hr>"; echo "<p>mysqli_close(\$db);</p>"; echo "<br>"; echo '</div>'; echo "<br>"; ?> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
php代码
实例
<meta charset=utf-8> <?php //1.连接数据库 require 'mysqli_connect.php'; //2.准备SQL语句 $sql = "DELETE FROM staff WHERE staff_id=?;"; //3.创建stmt对象 $stmt = mysqli_stmt_init($db); //4.检测SQL语句 if (mysqli_stmt_prepare($stmt, $sql)) { /* 参数绑定 */ mysqli_stmt_bind_param($stmt, "i", $id); $id = '1'; /* 执行SQL语句 */ mysqli_stmt_execute($stmt); echo '<br>删除了'.mysqli_stmt_affected_rows($stmt).'条记录'; // $id = '2'; // /* 执行SQL语句 */ // mysqli_stmt_execute($stmt); // echo '<br>删除了'.mysqli_stmt_affected_rows($stmt).'条记录'; // $id = '3'; // /* 执行SQL语句 */ // mysqli_stmt_execute($stmt); // echo '<br>删除了'.mysqli_stmt_affected_rows($stmt).'条记录'; } else { exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt)); } /* 注销stmt对象 */ mysqli_stmt_close($stmt); /* 关闭数据库连接 */ mysqli_close($db);
运行实例 »
点击 "运行实例" 按钮查看在线实例
用预处理方法完成修改操作
流程
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>用预处理方法完成修改操作</title> <style type="text/css"> .box {width:90%;line-height:1.2em;border:1px dotted #d6d6d6;padding-left:15px;background-color:#f5f5f5;margin:10px auto;font-family:微软雅黑;} .box1 {width:90%;line-height:1.5em;border:1px dotted #d6d6d6;background-color:#f2edd7;font-family:微软雅黑;} .box p {margin-left:20px;font-size:14px;} hr {height:1px;width:95%;border:none;border-top:1px dashed #666;} </style> </head> <body> <?php echo '<div class="box">'; echo '<h4>用预处理方法完成修改操作</h4>'; echo '<div class="box1">'; echo '步骤: * 1.连接数据库 * 2.准备SQL语句 * 3.创建stmt对象 * 4.检测SQL语句 (参数绑定 执行SQL语句 ) * 5.注销stmt对象 * 6.关闭数据库连接'; echo '</div>'; echo "<br>"; echo '<div class="box1"> <p>1. 创建连接参数</p>'; echo "<hr>"; echo "<p>require 'mysqli_connect.php';</p>"; echo "<br>"; echo '<p>2. 准备SQL语句</p>'; echo "<hr>"; echo "<p>\$sql = \"UPDATE staff SET gongzi=29000 WHERE staff_id=?;\";</p>"; echo "<br>"; echo '<p>3. 创建stmt对象</p>'; echo "<hr>"; echo "<p>\$stmt = mysqli_stmt_init(\$db);</p>"; echo "<br>"; echo '<p>4. 检测SQL语句 (参数绑定 执行SQL语句 )</p>'; echo "<hr>"; echo "<p>if (mysqli_stmt_prepare(\$stmt, $sql)) {</p>"; /* 参数绑定 */ echo "<p> mysqli_stmt_bind_param(\$stmt, \"i\", \$id);</p>"; echo "<p> \$id = '9';</p>"; /* 执行SQL语句 */ echo "<p> mysqli_stmt_execute(\$stmt);</p>"; echo "<p> echo '成功修改了'.mysqli_stmt_affected_rows(\$stmt).'条记录';</p>"; echo "<p>} else {</p>"; echo "<p> exit(mysqli_stmt_errno(\$stmt).':'.mysqli_stmt_error(\$stmt));</p>"; echo "<p>}</p>"; echo "<br>"; echo '<p>5. 注销stmt对象</p>'; echo "<hr>"; echo "<p>mysqli_stmt_close(\$stmt);</p>"; echo "<br>"; echo '<p>6.关闭数据库连接</p>'; echo "<hr>"; echo "<p>mysqli_close(\$db);</p>"; echo "<br>"; echo '</div>'; echo "<br>"; ?> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
PHP代码
实例
<?php //1.连接数据库 require 'mysqli_connect.php'; //2.准备SQL语句 $sql = "UPDATE staff SET gongzi=29000 WHERE staff_id=?;"; //3.创建stmt对象 $stmt = mysqli_stmt_init($db); //4.检测SQL语句 if (mysqli_stmt_prepare($stmt, $sql)) { /* 参数绑定 */ mysqli_stmt_bind_param($stmt, "i", $id); $id = '9'; /* 执行SQL语句 */ mysqli_stmt_execute($stmt); echo '<br>成功修改了'.mysqli_stmt_affected_rows($stmt).'条记录'; } else { exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt)); } /* 注销stmt对象 */ mysqli_stmt_close($stmt); /* 关闭数据库连接 */ mysqli_close($db);
运行实例 »
点击 "运行实例" 按钮查看在线实例