博主信息
博文 32
粉丝 0
评论 0
访问量 26017
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
使用预处理技术实现更新与删除操作-2018年5月1日21:26:39
inhylei
原创
877人浏览过

预处理实现更新数据

$sql = "UPDATE staff SET name=?,salary=? WHERE staff_id=?;";

$stmt = mysqli_stmt_init($db);

if (mysqli_stmt_prepare($stmt,$sql)){
    mysqli_stmt_bind_param($stmt,'sii',$name,$salary,$staff_id);
    $name = 'xiaolongnn';$salary = 5600;$staff_id=15;
    if(mysqli_stmt_execute($stmt)){
        if (mysqli_stmt_affected_rows($stmt)>0){
            echo '更新成功,记录:' . mysqli_stmt_affected_rows($stmt);
        }else{
            echo '没有更新记录';
        }

    }else{
        exit(mysqli_stmt_errno($stmt) . ':' . mysqli_stmt_error($stmt));

    }

}else{
    exit(mysqli_stmt_errno($stmt) . ':' . mysqli_stmt_error($stmt));

}
mysqli_stmt_close($stmt);
mysqli_close($db);

预处理实现删除数据

require 'mysqli-connect.php';
$sql = "DELETE FROM staff WHERE staff_id=?";

$stmt = mysqli_stmt_init($db);

if (mysqli_stmt_prepare($stmt,$sql)){
    mysqli_stmt_bind_param($stmt,'i',$staff_id);
    $staff_id = 19;
    if(mysqli_stmt_execute($stmt)){
        if (mysqli_stmt_affected_rows($stmt)>0){
            echo '删除成功:' . mysqli_stmt_affected_rows($stmt);
        }else{
            echo '删除失败';
        }

    }else{
        exit(mysqli_stmt_errno($stmt) . ':' . mysqli_stmt_error($stmt));

    }

}else{
    exit(mysqli_stmt_errno($stmt) . ':' . mysqli_stmt_error($stmt));

}
mysqli_stmt_close($stmt);
mysqli_close($db);

总结

预处理操作:使用mysqli_stmt_init()创建stmt对象

mysqli_stmt_prepare检测stmt对象

mysqli_stmt_bind_param()邦定stmt对象

执行stmt对象mysqli_stmt_excute()

mysqli_stmt_close($stmt);关闭预处理对象
mysqli_close($db);不要忘记关闭数据库


批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学