Blogger Information
Blog 36
fans 0
comment 1
visits 28098
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用预处理技术实现更新与删除操作
其琛的博客
Original
588 people have browsed it

更新代码

<?php
//    预处理技术实现更新操作
//第一步连接数据库
define('db_host','127.0.0.1');
define('db_user','root');
define('db_pas','root');
define('db_name','php');
define('db_char','utf8');

$db = mysqli_connect(db_host,db_user,db_pas);
//第二步准备SQL语句
$sql = "UPDATE class SET grade=? WHERE class_id=2";
$stmt = mysqli_stmt_init($db);
if (mysqli_stmt_prepare($stmt, $sql)){
    mysqli_stmt_bind_param($stmt, "l", $grade);
    $grade = 90;
    mysqli_stmt_execute($stmt);
    echo '<br>新增了'.mysqli_stmt_affected_rows($stmt).'条记录,主键是:'.mysqli_stmt_insert_id($stmt);
} else {
    exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt));
}
mysqli_stmt_close($stmt);
mysqli_close($db);

删除代码

<?php
define('db_host','127.0.0.1');
define('db_user','root');
define('db_pas','root');
define('db_name','php');
define('db_char','utf8');
$db = mysqli_connect(db_host,db_user,db_pas);
if ($db != false){
    echo '连接成功';
}else {
    echo '连接失败';
}
$sql = "DELETE IGNORE * FROM class WHERE name=?";
$stmt = mysqli_stmt_init($db);
if (mysqli_stmt_prepare($stmt, $sql)) {
    mysqli_stmt_bind_param($stmt, 'i',$name);
    $name='张三';
    mysqli_stmt_execute($stmt);
}else {
    exit(mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt));
}
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