内容:
改写预处理的更新操作,将外层的查询操作用预处理来实现;、
使用预处理实现MySQLi面向对象的删除操作;
配置文件
<?php
header("Content-type: text/html; charset=utf-8");
//创建配置文件
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','root');
define('DB_NAME','demo');
define('DB_CHAR','utf8');连接文件
<?php
include 'config.php';
//连接到数据库demo
$mysqli=new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
//连接是否成功,失败则返回错误信息
if ($mysqli->connect_error) {
die( "连接失败(".$mysqli->connect_errno.')'.$mysqli->connect_error);
}
$mysqli->set_charset(DB_CHAR);预处理更新
<?php
/**
* 任务: 更新id =5的记录
* 基本步骤:
* 1. 查询表中是否有id=5的记录,如果有就继续,否则提示用户
* 2. 确定只查询到一条记录,并获取到这个记录的数据
* 3. 准备要更新的数据,放在一个数组中
* 4. 创建预处理更新语句,并生成预处理对象
* 5. 执行预处理更新,并对执行结果进行分析
* 6. 关闭预处理语句
* 7. 关闭连接
*/
//连接数据库
require 'public/connect.php';
//查询更新项
$sql="SELECT `id`,`name`,`email` FROM `user1` WHERE `id` = ? ";
$id=5;
//预处理查询
$mysqli_stmt=$mysqli->prepare($sql);
//绑定查询信息
$mysqli_stmt->bind_param('i',$id);
//执行查询
if ($mysqli_stmt->execute()){
//返回数据
$mysqli_stmt->store_result();
if ($mysqli_stmt->num_rows()==1){
//绑定结果集字段与变量
$mysqli_stmt->bind_result($id,$name,$email);
echo '<h3 align="center">修改前的数据为</h3>';
echo '<table border="1" cellspacing="0" cellpadding="3" width="40%" align="center">';
echo '<tr bgcolor="lightblue"><th>ID</th><th>姓名</th><th>邮箱</th></tr>';
while($mysqli_stmt->fetch()){
echo '<tr align="center">';
echo '<td>'.$id.'</td><td>'.$name.'</td><td>'.$email.'</td>';
echo '</tr>';
}
$data = ['name'=>'Peter', 'email'=>'z@php.cn', 'password'=>'123'];
//3.创建更新的预处理SQL语句
$sql = "UPDATE user1 SET name=?, email=?, password=sha1(?) WHERE id = ?";
//4.用连接对象$mysqli的prepare()方法来创建一个预处理对象
$mysqli_stmt = $mysqli->prepare($sql);
//5.调用预处理对象中的bind_param()方法将实际参数与SQL语句中的占位符进行绑定
$mysqli_stmt->bind_param('sssi', $data['name'], $data['email'], $data['password'], $id);
//6. 执行预处理更新操作: execute(),返回布尔值,成功true,失败为false
if($mysqli_stmt->execute()) {
//如果更新成功,应该根据受影响的记录数量,再进行一次判断
if ($mysqli_stmt->affected_rows) { //如果更新成功,会返回整数: 1
echo '<h3>更新成功</h3>';
}else {
echo '<h3 style="color:red">没有记录被更新</h3>';
}
} else {
echo '<h3 style="color:red">更新失败'.$mysqli_stmt->error.'</h3>';
}
}else echo "<h3 style='color: red'>没有查到到数据</h3>";
}else echo "<h3 style='color: red'>查询失败".$mysqli_stmt->error."</h3>";
//关闭预处理语句
$mysqli_stmt->close();
//关闭连接
$mysqli->close();
预处理删除
<?php
/**
* 任务: 删除id =8的记录
* 基本步骤:
* 1. 查询表中是否有id=8的记录,如果有就继续,否则提示用户
* 2. 确定查询到记录,并获取到数据
* 3. 创建预处理更新语句,并生成预处理对象
* 4. 执行预处理更新,并对执行结果进行分析
* 5. 关闭预处理语句
* 6. 关闭连接
*/
//连接数据库
require 'public/connect.php';
//查询更新项
$sql="SELECT `id`,`name`,`email` FROM `user1` WHERE `id` = ? ";
$id=8;
//预处理查询
$mysqli_stmt=$mysqli->prepare($sql);
//绑定查询信息
$mysqli_stmt->bind_param('i',$id);
//执行查询
if ($mysqli_stmt->execute()){
//返回数据
$mysqli_stmt->store_result();
if ($mysqli_stmt->num_rows()>0){
//绑定结果集字段与变量
$mysqli_stmt->bind_result($id,$name,$email);
echo '<h3 align="center">删除数据为</h3>';
echo '<table border="1" cellspacing="0" cellpadding="3" width="40%" align="center">';
echo '<tr bgcolor="lightblue"><th>ID</th><th>姓名</th><th>邮箱</th></tr>';
while($mysqli_stmt->fetch()){
echo '<tr align="center">';
echo '<td>'.$id.'</td><td>'.$name.'</td><td>'.$email.'</td>';
echo '</tr>';
}
//3.创建删除的预处理SQL语句
$sql = "DELETE FROM user1 WHERE id = ?";
//4.用连接对象$mysqli的prepare()方法来创建一个预处理对象
$mysqli_stmt = $mysqli->prepare($sql);
//5.调用预处理对象中的bind_param()方法将实际参数与SQL语句中的占位符进行绑定
$mysqli_stmt->bind_param('i', $id);
//6. 执行预处理更新操作: execute(),返回布尔值,成功true,失败为false
if($mysqli_stmt->execute()) {
//如果更新成功,应该根据受影响的记录数量,再进行一次判断
if ($mysqli_stmt->affected_rows) { //如果更新成功,会返回整数: 1
echo '<h3>删除成功</h3>';
}else {
echo '<h3 style="color:red">没有记录被删除</h3>';
}
} else {
echo '<h3 style="color:red">删除失败'.$mysqli_stmt->error.'</h3>';
}
}else echo "<h3 style='color: red'>没有查到到数据</h3>";
}else echo "<h3 style='color: red'>查询失败".$mysqli_stmt->error."</h3>";
//关闭预处理语句
$mysqli_stmt->close();
//关闭连接
$mysqli->close();
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号