说明:
在“PHP + MySQL 增删查改(CURD)实例——面向过程方法”(http://hanyufeng.php.cn/blog/detail/2134)的基础上,改用面向对象及SQL语句预处理的方法实现。
重点:
mysqli、mysqli_stmt 对象的常用方法及属性。
预处理语句的参数写法。
公共文件修改:
connect.php
<?php
//使用配置文件保存参数
require 'config.php';
//使用mysqli对象
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($mysqli->connect_errno) {
printf("连接错误: %s". $mysqli->connect_error);
}
$mysqli->set_charset(DB_CHARSET);列表(查询)页面 list.php
<?php
$pageTitle = '用户信息列表';
include 'inc/header.php';
//连接数据库
require 'inc/connect.php';
//查询用户信息表user
$sql = "SELECT `id`,`name`,`email` FROM user"; //创建查询语句
//注意:不能写 SELECT * ,必须逐个写出
//否则报错提示:Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in……
$mysqli_stmt = $mysqli->prepare($sql);
$rows = []; //创建查询结果容器,初始为空数组,该数组最终会成为一个二维数组,与数组表对应
if ($mysqli_stmt->execute()) { //select语句执行会返回一个结果集
$mysqli_stmt->store_result(); //传送一个结果集到stmt对象
if ($mysqli_stmt->num_rows > 0) {
//将结果集中的字段与变量进行绑定,当前返回的有三个字段
$mysqli_stmt->bind_result($id,$name,$email);
//开始遍历结果集,将获取到的数据与绑定的变量结合
$i =0;
while ($mysqli_stmt->fetch()) { //从结果集的第一条记录开始抓取数据到绑定的变量中
//继续沿用$rows数组保存数据,以便重复使用输出数据到页面的代码
$rows[$i] = ['id'=>$id,'name'=>$name,'email'=>$email];
$i++;
}
$mysqli_stmt->free_result(); //释放预处理结果集
$mysqli_stmt->close(); //关闭当前的预处理语句
} else {
echo '<p style="color:red">当前表中没有数据~~</p>';
}
} else {
echo '<p>查询失败:'.$mysqli_stmt->error.'</p>';
}
//关闭连接
$mysqli->close();
?>编辑页面 edit.php
<?php include 'inc/header.php';
$id = $_GET['id']; //获取到要编辑的记录的id
require 'inc/connect.php'; //连接数据库
//准备查询语句
$sql = "SELECT * FROM `user` WHERE `id` = ".$id;
$res = $mysqli->query($sql); //执行查询
if(isset($res))
{
if($res->num_rows>0)
{
$row = $res->fetch_array();
}
}
//if ($res && mysqli_num_rows($res) > 0) {
// //根据主键查询只会返回一条记录,不用循环处理
// $row = mysqli_fetch_array($res, MYSQLI_ASSOC);
//}
?>do_edit.php 代码调整,使用预处理sql语句生成mysqli_stmt对象,并绑定参数
<?php
/**
* 1.检测表单数据有效性
* 2.更新数据表(使用预处理对象mysqli_stmt)
* 3.处理错误信息
*/
$pageTitle = '更新处理'; //设置当前页面标题
include 'inc/header.php'; //导入公共文件头部
//1.检测表单数据有效性
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$error = []; //创建一个数组,用来保存出错信息
//用户名只读,跳过检查
//检查邮箱信息
if (empty($_POST['email'])) {
$error[] = '没有输入邮箱,请检查';
} else {
$email = trim($_POST['email']);
}
//如果错误信息数组空,数据有效,更新到数据库
//2.更新数据表
if (empty($error)) {
//连接数据库
require 'inc/connect.php';
//创建预处理sql语句,参数使用?占位
$sql = "UPDATE user SET `email`=? WHERE `id`=?";
$mysqli_stmt = $mysqli->prepare($sql);
$id = $_GET['id'];
//参数类型: s字符串,i整数,d浮点小数
$mysqli_stmt->bind_param('si', $email, $id);
//更新数据
if ($mysqli_stmt->execute()){
echo '<h3 style="color: green">成功更新了'.$mysqli_stmt->affected_rows.'条记录</h3>';
} else {
echo '<h3 style="color:red">更新失败:'.$mysqli_stmt->error.'</h3>';
}
//关闭预处理语句
//如果重复使用预处理语句,就不需要close
$mysqli_stmt->close();
} else {
//打印出错误信息
foreach ($error as $message) {
echo '<h3 style="color:red">'.$message.'</h3>';
}
}
}
include 'inc/footer.php'; //导入公共头部delete.php
<?php
require 'inc/connect.php';
$sql = "DELETE FROM user WHERE id=?";
$mysqli_stmt = $mysqli->prepare($sql);
$id = $_GET['id'];
//参数类型: s字符串,i整数,d浮点小数
$mysqli_stmt->bind_param('i', $id);
if ($mysqli_stmt->execute()){
echo '<script>alert("成功删除'.$mysqli_stmt->affected_rows.'条记录")</script>';
} else {
echo '<script>alert("删除失败'.$mysqli_stmt->error.'")</script>';
}
$mysqli_stmt->close();
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号