今天,課上學(xué)習(xí)了員工管理系統(tǒng)的添加 刪除 修改,作業(yè)時(shí)作出軟刪除。廢話不多說(shuō)上代碼,談邏輯。
一、實(shí)現(xiàn)添加功能
在staff_list上的添加按鈕設(shè)置導(dǎo)航到,staff_add.php,然后新建staff_add.php,然后構(gòu)建前端頁(yè)面,在新增按鈕設(shè)置add方法傳入this.form,重點(diǎn)在js的add方法,add方法中要做ajax數(shù)據(jù)提交將form下的name age position sex mobile的值傳遞到staff_manage.php,這里需要注意的是,提交按鈕里面需要連接到staff_manage,將action=add通過(guò)get方法傳過(guò)來(lái);然后在staff_manage連接數(shù)據(jù)庫(kù),將cation復(fù)制給$action,switch判斷,當(dāng)action為add時(shí),那么就執(zhí)行添加數(shù)據(jù)到數(shù)據(jù)庫(kù)的操作,然后判斷,當(dāng)數(shù)據(jù)庫(kù)添加操作被執(zhí)行時(shí)——》(然后再進(jìn)行判斷,影響行數(shù)為1時(shí),status為1,message為添加數(shù)據(jù)成功;影響行數(shù)為0時(shí),status為0,message為沒(méi)有添加數(shù)據(jù));數(shù)據(jù)庫(kù)沒(méi)有執(zhí)行操作時(shí)——》(status為-1,message為操作錯(cuò)誤),判斷完成后,把status和message專程json格式傳回前端staff_add,staff_add頁(yè)面在ajax返回響應(yīng)數(shù)據(jù)階段,判斷如果status時(shí)1添加正確css樣式,否則為錯(cuò)誤樣式,然后把message賦給tips的內(nèi)容,再添加定時(shí)器要2秒鐘后返回index,至此添加功能完成。
<?php session_start(); ?> <?php if (!isset($_SESSION['username'])): ?> <style>h2,p{text-align: center;margin-top: 50px;}</style> <h2>您還沒(méi)有登錄呢~~</h2> <p>正在跳轉(zhuǎn)到登錄頁(yè)面...</p> <script> setTimeout("location.assign('login.php')", 2000); </script> <?php else:?> <!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>添加員工</title> <style> h3 { text-align: center; } div { width: 300px; height: 260px; /*background-color: lightblue;*/ margin: 0 auto; text-align: center; padding: 20px; border: 1px dashed #888; border-radius: 5%; } div input { background-color: transparent; border: none; border-bottom: 1px solid #333; } button:hover { cursor: pointer; background-color: lightblue; } .success { color: green; } .error { color: red; } </style> </head> <body> <h3>添加員工</h3> <div> <form name="user"> <p> <label for="name">姓名:</label> <input type="text" name="name" id="name" value=""> </p> <p> <label for="age">年齡:</label> <input type="number" name="age" id="age" value=""> </p> <p> <label for="position">職位:</label> <input type="text" name="position" id="position" value=""> </p> <p> <label for="mobile">手機(jī):</label> <input type="text" name="mobile" id="mobile" value=""> </p> <p style="margin-left: -55px;">性別: <input type="radio" id="male" name="sex" value="1" checked><label for="male">男</label> <input type="radio" id="female" name="sex" value="0"><label for="female">女</label> </p> <p> <button type="button" onclick="add(this.form)">新增</button> <button type="button" onclick="history.back()">取消</button> </p> <!-- 提示占位符--> <p></p> </form> </div> <script> function add(form) { // ajax方式交互數(shù)據(jù) var request = new XMLHttpRequest(); // 請(qǐng)求完成獲得相應(yīng)數(shù)據(jù) request.onreadystatechange = function () { if (request.readyState === 4 && request.status === 200) { // 將返回的json字符串解析成對(duì)象 var data = JSON.parse(request.responseText); // 獲取tips元素 var tips = form.lastElementChild; tips.innerText = data.message; // 判斷返回來(lái)的status值 1 成功 反之錯(cuò)誤 if (data.status === 1) { tips.classList.add('success'); } else { tips.classList.add('error'); } // 兩秒后 返回上一層也就是staff_list setTimeout(function(){ history.back(); top.location.reload(); },2000); } }; //配置請(qǐng)求信息 request.open('POST', 'staff_manage.php?action=add'); // ajax請(qǐng)求頭 request.setRequestHeader('content-type','application/x-www-form-urlencoded'); // 發(fā)送的數(shù)據(jù)集合成json數(shù)據(jù) var data = { name:form.name.value, age:form.age.value, position:form.position.value, mobile:form.mobile.value, sex:form.sex.value }; var staff = 'name=' + data.name + '&age=' + data.age + '&position=' + data.position + '&sex=' + data.sex + '&mobile=' + data.mobile; // 發(fā)送請(qǐng)求數(shù)據(jù) request.send(staff); } </script> </body> </html> <?php endif?>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
二、更新操作
更新操作總體思路和添加一樣,也是通過(guò)get傳給staff_manage一個(gè)action=save值 ,通過(guò)判斷save后然后在執(zhí)行更新操作,根據(jù)執(zhí)行結(jié)果返回響應(yīng)的status值和message值給前臺(tái),前臺(tái)在進(jìn)行相應(yīng)的樣式添加。這里的細(xì)節(jié)主要有,在staff_list的編輯按鈕通過(guò)隱藏域,把數(shù)據(jù)庫(kù)中的id用get方式傳遞到新建頁(yè)面staff_edit頁(yè)面,這也頁(yè)面的樣式同添加頁(yè)面一直,只不過(guò)表單的值時(shí)通過(guò)id然后數(shù)據(jù)庫(kù)查詢添加上去的,這里的修改提交按鈕還是通過(guò)ajax異步post傳給staff_manage,然后在進(jìn)行數(shù)據(jù)庫(kù)操作。
<?php //echo $_GET['id']; //連接數(shù)據(jù)庫(kù) $pdo = new PDO('mysql:dbname=php', 'root', 'root'); //獲取準(zhǔn)備對(duì)象 $stmt = $pdo->prepare("SELECT * FROM `staff` WHERE `id`=:id"); //$stmt->debugDumpParams(); //執(zhí)行操作 $stmt->execute(['id'=>$_GET['id']]); //獲取記錄 $staff = $stmt->fetch(PDO::FETCH_ASSOC); //print_r($staff); ?> <!doctype html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>編輯員工</title> <style> h3 { text-align: center; } div { width: 300px; height: 260px; /*background-color: lightblue;*/ margin: 0 auto; text-align: center; padding: 20px; border: 1px dashed #888; border-radius: 5%; } div input { background-color: transparent; border: none; border-bottom: 1px solid #333; color: #777; text-align: center; font-size: inherit; } button:hover { cursor: pointer; background-color: lightblue; } .success { color: green; } .error { color: red; } </style> </head> <body> <h3>編輯員工</h3> <div> <form name="user"> <p> <label for="name">姓名:</label> <input type="text" name="name" id="name" value="<?=$staff['name']?>"> </p> <p> <label for="age">年齡:</label> <input type="number" name="age" id="age" value="<?=$staff['age']?>"> </p> <p> <label for="position">職位:</label> <input type="text" name="position" id="position" value="<?=$staff['position']?>"> </p> <p> <label for="mobile">手機(jī):</label> <input type="text" name="mobile" id="mobile" value="<?=$staff['mobile']?>"> </p> <p style="margin-left: -55px;">性別: <input type="radio" id="male" name="sex" value="1"><label for="male">男</label> <input type="radio" id="female" name="sex" value="0"><label for="female">女</label> </p> <script> var male = document.getElementById('male'); var female = document.getElementById('female'); // 將male female的值變成整數(shù) 然后判斷是否checked if (parseInt(male.value) === <?= $staff['sex']?>) { male.checked = true; } if (parseInt(female.value) === <?= $staff['sex']?>) { female.checked = true; } </script> <p><input type="hidden" name="id" value="<?= $staff['id']?>"></p> <p> <button type="button" onclick="save(this.form)">修改</button> <button type="button" onclick="history.back()">取消</button> </p> <!-- 提示占位符--> <p></p> </form> </div> </body> <script> function save(form) { // 用ajax方式將數(shù)據(jù)異步提交 var request = new XMLHttpRequest(); // 請(qǐng)求完成獲得響應(yīng)數(shù)據(jù) request.onreadystatechange = function () { if (request.readyState === 4 && request.status === 200) { var data = JSON.parse(request.responseText); var tips = form.lastElementChild; tips.innerText = data.message; if (data.status === 1) { tips.classList.add('success'); } else { tips.classList.add('error'); } setTimeout(function () { history.back(); top.location.reload(); }, 2000); } }; // 配置請(qǐng)求信息 request.open('POST', "staff_manage.php?action=save"); // post方式請(qǐng)求頭 request.setRequestHeader('content-type','application/x-www-form-urlencoded'); // 設(shè)置數(shù)據(jù) var data = { name:form.name.value, age:form.age.value, position:form.position.value, sex:form.sex.value, mobile:form.mobile.value, id:form.id.value }; var staff = 'name=' + data.name + '&age=' + data.age + '&position=' + data.position + '&sex=' + data.sex + '&mobile=' + data.mobile + '&id=' + data.id; // 發(fā)送請(qǐng)求數(shù)據(jù) request.send(staff); } </script> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
三、刪除操作
在staff_list里的按鈕設(shè)置超鏈接 confirm(‘是否刪除’)?location.assign('staff_manage.php?action=del&id=<?=$staff['id']?>') : false" 然后在staff_manage del方法,如果執(zhí)行成功就彈窗刪除成功。
<?php //連接數(shù)據(jù)庫(kù) $pdo = new PDO('mysql:dbname=php','root','root'); //獲取action的get傳值 $action = strtolower(trim($_GET['action'])); //根據(jù)action值判斷應(yīng)該采用的處理方法 switch ($action) { // 新增方法 case 'add': // 新增sql語(yǔ)句 $sql = 'INSERT INTO `staff` (`name`, `age`, `sex`, `position`, `mobile`, `hiredate`) VALUES (:name, :age, :sex, :position, :mobile, :hiredate);'; // 準(zhǔn)備對(duì)象 $stmt = $pdo->prepare($sql); // 將post傳過(guò)來(lái)的值賦給相應(yīng)的變量 $name = trim($_POST['name']); $age = trim($_POST['age']); $sex = trim($_POST['sex']); $position = trim($_POST['position']); $mobile = trim($_POST['mobile']); $hiredate = time(); // 數(shù)據(jù)綁定 $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); // 如果執(zhí)行成功,切影響行數(shù)等于1那么就是新增成功了 if($stmt->execute()){ if ($stmt->rowCount() == 1) { $status = 1; $message = '新增成功'; } elseif ($stmt->rowCount() == 0) { $status = 0; $message = '沒(méi)有新增'; } } else { $status = -1; $message = '發(fā)生錯(cuò)誤'; } // 輸出json格式的status message值 echo json_encode(['status'=>$status, 'message'=>$message]); break; case 'save': $sql = "UPDATE `staff` SET `name`=:name, `age`=:age, `position`=:position, `sex`=:sex, `mobile`=:mobile WHERE `id`=:id"; $stmt = $pdo->prepare($sql); $name = trim($_POST['name']); $age = trim($_POST['age']); $position = trim($_POST['position']); $sex = trim($_POST['sex']); $mobile = trim($_POST['mobile']); $id = trim($_POST['id']); $stmt->bindValue('name', $name, PDO::PARAM_STR); $stmt->bindValue('age', $age, PDO::PARAM_INT); $stmt->bindValue('position', $position, PDO::PARAM_STR); $stmt->bindValue('sex', $sex, PDO::PARAM_INT); $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 = '沒(méi)有更新數(shù)據(jù)'; } } else { $status = -1; $message = '更新發(fā)送錯(cuò)誤'; } 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('刪除成功');location.assign('index.php')</script>"; }*/ /*軟刪除*/ $sql = "UPDATE `staff` SET `is_show`=0 WHERE `id`=:id"; $stmt = $pdo->prepare($sql); if ($stmt->execute(['id'=>$_GET['id']])) { echo "<script>alert('刪除成功');location.assign('index.php')</script>"; } }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
四、作業(yè):軟刪除
通過(guò)在數(shù)據(jù)庫(kù)添加is_show字段,默認(rèn)值為1顯示,0為不顯示,然后在staff_manage中的del中設(shè)置為id對(duì)應(yīng)的數(shù)據(jù)的is_show值為0,在staff_list設(shè)置is_show的為1的才顯示 就成了
五、總結(jié)
添加刪除修改 這三個(gè)操作相同的都是通過(guò)前端相應(yīng)的數(shù)據(jù)傳輸?shù)胶蠖?然后后段判斷 進(jìn)行相應(yīng)的操做,不同的是添加和修改都需要頁(yè)面展示。
我需要注意的還是對(duì)js的操作,主要對(duì)ajax的操作,對(duì)超鏈接的定向location 還有便捷的找到相應(yīng)的元素不熟悉
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)