Blogger Information
Blog 42
fans 0
comment 1
visits 25998
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
add和update作业-2018年4月25日下午15:00完成
邵军-山东-84918的博客
Original
633 people have browsed it

add代码:

实例

<?php
//1.连接数据库
require 'connect.php';
// 删除第二个数据
// 2.准备SQL语句,将变量部分使用占位符进行代替
$sql="INSERT IGNORE number SET name=?,tel=?,home=?;";
$name="wang";
$tel="13555555555";
$home="shanghai";
//3.创建并初始化预处理对象stmt
$stmt=mysqli_stmt_init($con);
// 4.用stmt对象检测预处理语句是否正确,成功返回true,错误返回false
if(mysqli_stmt_prepare($stmt,$sql)){
    /* 将变量与SQL语句中的占位符进行绑定 */
mysqli_stmt_bind_param($stmt,"sss",$name,$tel,$home);
// 执行SQL语句
if(mysqli_stmt_execute($stmt)){
    if(mysqli_stmt_affected_rows($stmt)){
        echo "<script>alert('新增成功!')</script>";
    }
    else {
           echo "<script>alert('新增失败!')</script>";
       }
}

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

else { //返回SQL检测阶段的错误
    exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt));
}
// 注销stmt对象
mysqli_stmt_close($stmt);
// 关闭数据库连接
mysqli_close($con);

运行实例 »

点击 "运行实例" 按钮查看在线实例

update代码:

实例

<?php
//1.连接数据库
require 'connect.php';
// 删除第二个数据
// 2.准备SQL语句,将变量部分使用占位符进行代替
$sql="UPDATE  number SET  tel=? WHERE name=?";
$tel="13854687521";
$name="wang";
//3.创建并初始化预处理对象stmt
$stmt=mysqli_stmt_init($con);
// 4.用stmt对象检测预处理语句是否正确,成功返回true,错误返回false
if(mysqli_stmt_prepare($stmt,$sql)){
    /* 将变量与SQL语句中的占位符进行绑定 */
mysqli_stmt_bind_param($stmt,"ss",$tel,$name);
// 执行SQL语句
if(mysqli_stmt_execute($stmt)){
    if(mysqli_stmt_affected_rows($stmt)){
        echo "<script>alert('修改成功!')</script>";
    }
    else {
           echo "<script>alert('修改失败!')</script>";
       }
}

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

else { //返回SQL检测阶段的错误
    exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt));
}
// 注销stmt对象
mysqli_stmt_close($stmt);
// 关闭数据库连接
mysqli_close($con);

运行实例 »

点击 "运行实例" 按钮查看在线实例

总结就不写了:老师讲的很清楚了,逻辑很明晰,按照套路来吧。

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