使用软删除技术,来实现对员工信息中的记录删除操作
三个要点:
1.员工表中添加一个is_show字段,用来控制员工信息的显示与隐藏,
2.删除操作的SQL语句 不是用DELETE删除记录,而是用UPDATE语句更新is_show字段的值1:显示,0:隐藏
3.在staff_list.php显示输出员工记录的时候再增加一个条件(判断is_show的值 ,如果值是1,就显示出来,如果是0就不显示,表示删除了这条记录)
实例(staff_manage.php)
<?php $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); $action = strtolower(trim($_GET['action'])); //根据cation的值,执行对应的操作 switch ($action) { case 'add': //sql $sql = 'INSERT INTO `staff` (`name`,`age`,`sex`,`position`,`mobile`,`hiredate`) VALUES (:name,:age,:sex,:position,:mobile,:hiredate)'; $stmt = $pdo->prepare($sql); $name = trim($_POST['name']); $age = trim($_POST['age']); $sex = trim($_POST['sex']); $position = trim($_POST['position']); $mobile = trim($_POST['mobile']); $hiredate = time(); //绑定参数 $stmt->bindValue(':name',$name,PDO::PARAM_STR); $stmt->bindValue(':age',$age,PDO::PARAM_INT); $stmt->bindValue(':sex',$sex,PDO::PARAM_INT); $stmt->bindValue(':position',$position,PDO::PARAM_STR); $stmt->bindValue(':mobile',$mobile,PDO::PARAM_STR); $stmt->bindValue(':hiredate',$hiredate,PDO::PARAM_INT); if ($stmt->execute()) { if ($stmt->rowCount() === 1) { $status = 1; $message = '新增成功'; } else if ($stmt->rowCount() === 0) { $status = 0; $message = '没有记录被新增'; } } else { $status = -1; $message = '新增失败,请检查'; } echo json_encode(['status'=>$status, 'message'=>$message]); break; case 'save': $sql = 'UPDATE `staff` SET `name`=:name, `age`=:age, `sex`=:sex, `position`=:position, `mobile`=:mobile WHERE `id`=:id'; $stmt = $pdo->prepare($sql); $name = trim($_POST['name']); $age = trim($_POST['age']); $sex = trim($_POST['sex']); $position = trim($_POST['position']); $mobile = trim($_POST['mobile']); $id = $_POST['id']; //绑定参数 $stmt->bindValue(':name',$name,PDO::PARAM_STR); $stmt->bindValue(':age',$age,PDO::PARAM_INT); $stmt->bindValue(':sex',$sex,PDO::PARAM_INT); $stmt->bindValue(':position',$position,PDO::PARAM_STR); $stmt->bindValue(':mobile',$mobile,PDO::PARAM_STR); $stmt->bindValue(':id',$id,PDO::PARAM_INT); if ($stmt->execute()) { if ($stmt->rowCount() === 1) { $status = 1; $message = '更新成功'; } else if ($stmt->rowCount() === 0) { $status = 0; $message = '没有记录被更新'; } } else { $status = -1; $message = '更新失败,请检查'; } echo json_encode(['status'=>$status, 'message'=>$message]); break; //删除操作 /* case 'del': $sql = 'DELETE FROM `staff` WHERE `id`=:id'; $stmt = $pdo->prepare($sql); if ($stmt->execute(['id'=>$_GET['id']])) { echo '<script>alert("删除成功");top.location.assign("index.php");</script>'; } break;*/ //利用软删除技术来实现删除 case 'del': $sql = 'UPDATE `staff` SET `is_show`=0 WHERE `id`=:id'; $stmt = $pdo->prepare($sql); if ($stmt->execute(['id'=>$_GET['id']])) { echo '<script>alert("删除成功");top.location.assign("index.php");</script>'; } break; }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例 (staff_list.php)
<?php // 员工信息 //1.连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //2.执行的操作 $sql = 'SELECT * FROM `staff` WHERE `is_show`=1 LIMIT 5'; $stmt = $pdo->prepare($sql); $stmt->execute(); $staffs = $stmt->fetchAll(PDO::FETCH_ASSOC); //3.关闭连接 $pdo = null; // 标题 $title = '员工信息表'; // 表格标题 $tableTitle = $title; // 员工数量 $total = count($staffs); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title><?php echo $title; ?></title> <style> table,th,td { border: 1px solid #666; padding: 8px; } table { border-collapse: collapse; width: 80%; text-align: center; margin: 30px auto; } thead tr:first-of-type { background-color: lightblue; } tbody tr:hover { background-color: #efefef; } table > caption { font-size: 1.2rem; margin-bottom: 15px; } table + p { text-align: center; } button:hover { cursor: pointer; background-color: lightblue; } /*添加按钮给个特殊样式*/ #add { height: 25px; width: 90px; position: absolute; left: 650px; top: 40px; } </style> </head> <body> <button onclick="location.href='staff_add.php'" id="add">添加</button> <table> <caption> <?php echo '<span style="color:red">' . $tableTitle . '</span>'; ?> </caption> <thead> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>手机</th> <td>入职</td> <th>操作</th> </tr> </thead> <tbody> <!--foreach()替代语法--> <?php foreach($staffs as $staff) : ?> <tr> <td><?php echo $staff['id']; ?></td> <td><?php echo $staff['name']; ?></td> <td><?php echo $staff['age']; ?></td> <!--if()替代语法--> <td> <?php if($staff['sex'] == 1) : ?> 男 <?php else: ?> 女 <?php endif; ?> </td> <!--如果只是简单的输出变量可以使用php短标签语法--> <td><?=$staff['mobile']?></td> <td> <?php echo date('Y/m/d',$staff['hiredate']); ?> </td> <td> <button onclick="location.href='staff_edit.php?id=<?=$staff['id']?>'">编辑</button> <button onclick="return confirm('是否删除?') ? location.assign('staff_manage.php?action=del&id=<?=$staff['id']?>') : false"><span style="color:red">删除</span></button> </td> </tr> <?php endforeach;?> </tbody> </table> <p>总计: <?php echo $total; ?> 人</p> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例