Blogger Information
Blog 32
fans 0
comment 0
visits 21448
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用预处理技术实现更新与删除操作-2018年5月1日21:26:39
inhylei
Original
728 people have browsed it

预处理实现更新数据

$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);不要忘记关闭数据库


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post