创建一张数据表并练习数据库的写操作(新增/更新/删除)

Original 2019-04-19 10:53:27 272
abstract:<?php //创建一张数据表并练习数据库的写操作(新增/更新/删除) //1.创建PDO,连接数据库 $pdo=new PDO('mysql:host=127.0.0.1;dbname=php_edu','root','root'); //创建sql语句 //新增 //$sql="INSERT INTO 
<?php
//创建一张数据表并练习数据库的写操作(新增/更新/删除)
//1.创建PDO,连接数据库
$pdo=new PDO('mysql:host=127.0.0.1;dbname=php_edu','root','root');
//创建sql语句
//新增
//$sql="INSERT INTO `admin`(`name`,`root`,`email`,`password`,`create_time`)  VALUES (:name,:root,:email,:password,:create_time)";
//删除
//$sql="DELETE FROM `admin` WHERE `admin_id`=:admin_id";
//更新
$sql="UPDATE `admin` SET `root`=:root,`email`=:email WHERE `admin_id`=:admin_id";
//验证SQL语句,创建预处理对象
$stmt=$pdo->prepare($sql);
//绑定参数:新增
// $name='双儿';
// $root=1;
// $email='se@qq.com';
// $password=sha1(123456);
// $createTime=time();
// $name='建宁';
// $root=1;
// $email='jn@qq.com';
// $password=sha1(123456);
// $createTime=time();
// $stmt->bindParam(':name',$name,PDO::PARAM_STR,20);
// $stmt->bindParam(':root',$root,PDO::PARAM_INT,2);
// $stmt->bindParam(':email',$email,PDO::PARAM_STR,100);
// $stmt->bindParam(':password',$password,PDO::PARAM_STR,40);
// $stmt->bindParam(':create_time',$createTime,PDO::PARAM_INT,10);
 
//绑定参数:删除
// $id=1;
// $stmt->bindParam(':admin_id',$id,PDO::PARAM_INT);
 
//参数绑定:更新
$id=2;
$root=0;
$email='wj@qq.cn';
 
$stmt->bindParam(':admin_id',$id,PDO::PARAM_INT);
$stmt->bindParam(':root',$root,PDO::PARAM_INT);
$stmt->bindParam(':email',$email,PDO::PARAM_STR,100);
//执行:添加
// if($stmt->execute()){
//     echo ($stmt->rowCount()>0) ? '成功的添加了'.$stmt->rowCount().'条数据':'没有数据被更新';
// }else{
//     exit(print_r($stmt->errorInfo(),true));
// }
 
//执行:删除
// if($stmt->execute()){
//     echo ($stmt->rowCount()>0) ? '成功的删除了'.$stmt->rowCount().'条数据':'没有数据被删除';
// }else{
//     exit(print_r($stmt->errorInfo(),true));
// }
 
//执行:更新
if($stmt->execute()){
    echo ($stmt->rowCount()>0) ? '成功的更新了'.$stmt->rowCount().'条数据':'没有数据被更新';
}else{
    exit(print_r($stmt->errorInfo(),true));
}


Correcting teacher:天蓬老师Correction time:2019-04-19 13:38:54
Teacher's summary:这每一步具体是什么意思, 相信你知道 了, 但还要有一个熟悉的过程 , 只有通过大量的练习才可以做到的

Release Notes

Popular Entries