批改状态:未批改
老师批语:
<?php
//mysqli中的预处理技术
//简易的说就是把 sql语句分成两个部分处理 一部分呢是模板 一部分是变量
//这样子操作的方式可以有效的防止sql注入攻击
// 基本步骤
//1、创建预处理对象 stmt
//2、检测sql语句
//3、参数绑定
//4、执行查询
//5、释放预处理对象、
//6、关闭数据库连接
//加载配置文件
require 'connect.php';
//准备sql语句
$sql = "UPDATE loot2017_members SET coin=? WHERE uid = 325";
$coin = 2000;
//创建并初始化对象
$stmt = mysqli_stmt_init($dbc);
//检测sql语句是否合法
if(mysqli_stmt_prepare($stmt, $sql)){
//绑定参数
mysqli_stmt_bind_param($stmt, 'i',$coin);
// 执行sql语句
if (mysqli_execute($stmt)) {
echo '受影响的行数是'.mysqli_stmt_affected_rows($stmt);
} else {
echo mysqli_stmt_errno($stmt).mysqli_stmt_error($stmt);
}
} else {
echo mysqli_stmt_errno($stmt).mysqli_stmt_error($stmt);
}
//关闭对象
mysqli_stmt_close($stmt);
//关闭数据库
mysqli_close($dbc);
?>点击 "运行实例" 按钮查看在线实例
//加载配置文件
require 'connect.php';
//准备sql语句
$sql = "DELETE FROM loot2017_members WHERE uid = ?;";
$uid = 320;
//创建并初始化对象
$stmt = mysqli_stmt_init($dbc);
//检测sql语句是否合法
if(mysqli_stmt_prepare($stmt, $sql)){
//绑定参数
mysqli_stmt_bind_param($stmt, 'i',$uid);
// 执行sql语句
if (mysqli_execute($stmt)) {
echo '受影响的行数是'.mysqli_stmt_affected_rows($stmt);
} else {
echo mysqli_stmt_errno($stmt).mysqli_stmt_error($stmt);
}
} else {
echo mysqli_stmt_errno($stmt).mysqli_stmt_error($stmt);
}
//关闭对象
mysqli_stmt_close($stmt);
//关闭数据库
mysqli_close($dbc);
?>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号