Correction status:qualified
Teacher's comments:
使用PDO进行数据表的新增,更新与删除操作练习
1、更新操作
<?php $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); $sql ='UPDATE `staff` SET `position` = :position WHERE `id` = :id'; $stmt = $pdo->prepare($sql); $stmt->bindValue('position','文化馆长',PDO :: PARAM_STR); $stmt->bindValue('id',5,PDO::PARAM_INT); $stmt->execute(); if($stmt->rowCount()>0){ echo "成功的更新了".$stmt->rowCount().'条记录'; } $pdo = null; ?>
点击 "运行实例" 按钮查看在线实例
2、删除操作
<?php $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); $sql ='DELETE FROM `staff` WHERE `id` = :id'; $stmt = $pdo->prepare($sql); $stmt->execute(['id'=>4]); if($stmt->rowCount()>0){ echo "成功的删除了".$stmt->rowCount().'条记录'; } $pdo = null; ?>
点击 "运行实例" 按钮查看在线实例
添加操作
<?php $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); $sql ='INSERT INTO `staff`(`name`,`age`,`sex`,`position`,`mobile`,`hiredate`) VALUES (:name,:age,:sex,:position,:mobile,:hiredate)'; $stmt = $pdo->prepare($sql); $name = '花二娘'; $age = 30; $sex = 1; $position = '莲花教主'; $mobile = '13473921234'; $hiredate = time(); $stmt->bindParam(':name',$name,PDO::PARAM_STR,20); $stmt->bindParam(':age',$age,PDO::PARAM_INT); $stmt->bindParam(':sex',$sex,PDO::PARAM_INT); $stmt->bindParam(':position',$position,PDO::PARAM_STR,20); $stmt->bindParam(':mobile',$mobile,PDO::PARAM_STR,11); $stmt->bindParam(':hiredate',$hiredate,PDO::PARAM_INT); $stmt->execute(); if($stmt->rowCount()>0){ echo "成功的添加了".$stmt->rowCount().'条记录'; } $pdo = null; ?>
点击 "运行实例" 按钮查看在线实例