代碼知識:
iframe的name屬性配合a標(biāo)簽的target屬性實現(xiàn)頁面局部刷新:
a標(biāo)簽的target屬性的值指向iframe的name值則會實現(xiàn)
<a href="user/pages/user.php" target="frame">
<iframe src="index.php" name="frame" frameborder="0"></iframe>
PDO連接數(shù)據(jù)庫步驟:
$pdo = new PDO("mysql:host=127.0.0.1;dbname=php;",'root','root'); // 一步創(chuàng)建連接(實例化PDO類的實例對象)
$stmt = $pdo->prepare("select * from staff where id = :id"); // 二步創(chuàng)建預(yù)處理
$stmt->bindParam(['id'=>$id]); // 三步綁定參數(shù)
$stmt->execute(); // 四步執(zhí)行預(yù)處理語句
$user = $stmt->fetch(PDO::FETCH_ASSOC); // 五步獲取數(shù)據(jù)
$pdo = null; // 六步關(guān)閉數(shù)據(jù)庫
************************************************************************************************************
SQL語句:
**查詢:** select 字段列表 from 表名 where 查詢條件 order by 排序 limit 數(shù)量
**更新:** update 表名 set 列明=新值,... where 更新條件
**新增:** insert into 表名 (字段列表) values (新值列表)
如果僅針對MySQL數(shù)據(jù)庫,一下語法效率更高:
insert into 表名 set 列名=新值,列明=新值...
**刪除:** delete from 表名 where 刪除條件
參數(shù)綁定(三種方式):
execute([':id'=>4]) execute(['id'=>4]) 命名占位符的冒號可寫可不寫
bindParam(命名占位符,變量,[格式]):綁定一個參數(shù)到指定的變量名(類似于占位符)
bindValue(命名占位符,值,[格式]):把一個值綁定到一個參數(shù)(與bindParam類似)
格式:
FETCH_ASSOC:把一條記錄遍歷到關(guān)聯(lián)數(shù)組中
FETCH_NUM:把一條記錄遍歷到索引型數(shù)組中
FETCH_BOTH:把一條記錄遍歷到混合型數(shù)組中
FETCH_OBJ:把一條記錄遍歷到對象中
FETCH_BOUND (bindColumn):把某個變量綁定到結(jié)果集中的某個列
獲取數(shù)據(jù):
fetch( [格式] ) 獲取滿足數(shù)據(jù)的第一條數(shù)據(jù) 與while結(jié)合使用 指針會自動下移 一維數(shù)組返回
fetchAll( [格式] ) 獲取所有數(shù)據(jù) 與分頁結(jié)合使用 二維數(shù)組返回
格式:
FETCH_ASSOC:返回一個索引為結(jié)果集列名的數(shù)組
FETCH_BOTH(默認(rèn)):返回一個索引為結(jié)果集列名和以0開始的列號的數(shù)組
FETCH_BOUND:返回 TRUE ,并分配結(jié)果集中的列值給 PDOStatement::bindColumn() 方法綁定的 PHP 變量。
FETCH_CLASS:返回一個請求類的新實例,映射結(jié)果集中的列名到類中對應(yīng)的屬性名。如果 fetch_style 包含
FETCH_CLASSTYPE(例如:PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE),則類名由第一列的值決定
FETCH_INTO:更新一個被請求類已存在的實例,映射結(jié)果集中的列到類中命名的屬性
FETCH_LAZY:結(jié)合使用PDO::FETCH_BOTH和PDO::FETCH_OBJ,創(chuàng)建供用來訪問的對象變量名
FETCH_NUM:返回一個索引為以0開始的結(jié)果集列號的數(shù)組
FETCH_OBJ:返回一個屬性名對應(yīng)結(jié)果集列名的匿名對象
************************************************************************************************************
代碼:
首頁代碼: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="static/layui/css/layui.css" media="all"> <link rel="stylesheet" href="static/css/style.css" media="all"> </head> <body> <div class="container"> <div class="layui-row cont-head"> <div class="layui-col-md2"> <h2 class="center">員工管理系統(tǒng)</h2> </div> </div> <div class="layui-row cont-main"> <div class="layui-col-md2 cont-left" id='cont-left'> <ul> <li><a href="user/pages/index.php" target="frame"><i class='layui-icon layui-icon-home'></i> 首頁</a></li> <li><a href="user/pages/user.php" target="frame"><i class='layui-icon layui-icon-username'></i> 員工信息</a></li> <li><a href="user/pages/change.php" target="frame"><i class='layui-icon layui-icon-survey'></i> 人事變動</a></li> <li><a href="user/pages/approval.php" target="frame"><i class='layui-icon layui-icon-form'></i> 審批事項 <span class="layui-badge">6</span></a></li> <li><a href="user/pages/administration.php" target="frame"><i class='layui-icon layui-icon-set-fill'></i> 用戶管理</a></li> </ul> </div> <div class="layui-col-md10 cont-right"> <iframe src="user/pages/index.php" id='frame' name="frame" frameborder="0"></iframe> </div> </div> <p class="center bq">版權(quán)所有 翻版必究</p> </div> </body> </html> 員工信息代碼: <?php $thead = '員工信息'; $pdo = new PDO('mysql:host=127.0.0.1;dbname=php;','root','root'); $stmt = $pdo->prepare('select * from staff'); $res = $stmt->execute(); $users = $stmt->fetchAll(PDO::FETCH_ASSOC); $pdo = null; $num = 1; ?> <link rel="stylesheet" href="../../static/layui/css/layui.css" media="all"> <link rel="stylesheet" href="../../static/css/style.css" media="all"> <style> h3{padding:10px 0;font-size:20px;} </style> <script src="../../static/js/jquery.js"></script> <script src="../../static/layui/layui.all.js"></script> <h3 class="center"><?=$thead ?></h3> <table class="layui-table" id='table' lay-even> <thead> <tr> <th>ID</th> <th>姓名</th> <th>性別</th> <th>年齡</th> <th>郵箱</th> <th>工資</th> <th>操作</th> </tr> </thead> <tbody> <!-- 使用 foreach 替代語法遍歷數(shù)組 --> <?php foreach($users as $user): ?> <tr> <td data-id="<?=$user['id'] ?>"><?=$num++ ?></td> <td data-name="<?=$user['name'] ?>"><?=$user['name'] ?></td> <!-- 使用 if 替代語法 --> <td data-sex="<?=$user['sex'] ?>"> <?php if($user['sex']==1): ?> 男 <?php else: ?> 女 <?php endif; ?> </td> <td data-age="<?=$user['age'] ?>"><?=$user['age'] ?></td> <td data-email="<?=$user['email'] ?>"><?=$user['email'] ?></td> <td data-salary="<?=$user['salary'] ?>"><?=$user['salary'] ?></td> <td> <button class="layui-btn layui-btn-normal layui-btn-xs" onclick="location.href='user_edit.php?id=<?=$user['id'] ?>'"><i class='layui-icon layui-icon-edit'></i></button> <button class="layui-btn layui-btn-danger layui-btn-xs delete"><i class="layui-icon layui-icon-delete"></i></button> </td> </tr> <?php endforeach; ?> </tbody> </table> <script> layui.use(['layer','form'], function(){ var layer = layui.layer; var form = layui.form; form.render(); //監(jiān)聽提交 form.on('submit(formDemo)', function(data){ layer.msg(JSON.stringify(data.field)); return false; }); $("#table").delegate(".delete","click",function(){ var tr = $(this).parents('tr').index(); layer.open({ title:'刪除', type: 0, content: '確定刪除此記錄?', yes: function(index, layero){ $("#table tbody tr").eq(tr).remove(); console.log($("#table tbody tr").eq(tr)); layer.close(index); } }); }); }); </script> 員工信息編輯頁面代碼: <?php $id = intval(trim($_GET['id'])); $pdo = new PDO("mysql:host=127.0.0.1;dbname=php;",'root','root'); $stmt = $pdo->prepare("select * from staff where id ={$id}"); $stmt->execute(); $user = $stmt->fetch(PDO::FETCH_ASSOC); $pdo = null; ?> <link rel="stylesheet" href="../../static/layui/css/layui.css" media="all"> <script src="../../static/js/jquery.js"></script> <script src="../../static/layui/layui.all.js"></script> <style> .login{width:350px;margin:50px auto;} .login h3{text-align:center;line-height:50px;font-size:24px;font-weight:bold;} .success{color:#5FB878;} .error{color:#FF5722;} .notice{color:#FFB800;} </style> <div class='login'> <h3>員工信息編輯</h3> <form action="" class="layui-form" id='edit-form' name='user'> <div class="layui-form-item"> <label class="layui-form-label">用戶名</label> <div class="layui-input-inline"> <input type="text" name="name" value="<?=$user['name']; ?>" readonly lay-verify="required" placeholder="請輸入用戶名" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">郵 箱</label> <div class="layui-input-inline"> <input type="email" name="email" value="<?=$user['email']; ?>" required lay-verify="required|email" placeholder="請輸入郵箱" autocomplete="off" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">密 碼</label> <div class="layui-input-inline"> <input type="password" name="password" value="<?=$user['password']; ?>" required lay-verify="required" placeholder="請輸入密碼" autocomplete="off" class="layui-input"> </div> </div> <input type="hidden" name='id' value="<?=$id ?>" /> <div class="layui-form-item"> <label class="layui-form-label"> </label> <div class="layui-input-inline"> <button class="layui-btn" onclick="save(this.form)">更新</button> <button class="layui-btn layui-btn-primary" onclick='history.back();'>取消</button> </div> </div> <h4 id='tips' class='success'></h4> </form> </div> <script> layui.use(['layer','form'], function(){ var layer = layui.layer; var form = layui.form; form.render(); function save(form){ // 以 ajax 異步方式與服務(wù)器進(jìn)行數(shù)據(jù)交互 // 1、創(chuàng)建ajax對象 var request = new XMLHttpRequest(); // 2、監(jiān)聽成功回調(diào) request.onreadystatechange = function(){ // 成功返回的信息 if(request.readyState===4 && request.status===200){ // console.log(request.responseText); // request.responseText === $_POST 響應(yīng)返回的信息 是user_manager.php 文件內(nèi)容 var data = JSON.parse(request.responseText); // JSON.parse 將服務(wù)器傳輸?shù)男畔⑥D(zhuǎn)為json對象 var tips = form.lastElementChild; tips.innerHTML = data.message; console.log(tips); if(data.status === 1){ tips.classList.add('success'); }else{ tips.classList.add('error'); } setTimeout(function() { history.back(); }, 2000); } } // 3、初始化參數(shù) request.open('POST','user_manager.php?action=save'); // post 請求 需要設(shè)置一個請求頭 request.setRequestHeader('content-type','application/x-www-form-urlencoded'); // 發(fā)送請求到服務(wù)器 // 以鍵值對: var data = 'email='+form.email.value+'&password='+form.password.value+'&id='+form.id.value; request.send(data); } </script> 數(shù)據(jù)處理文件代碼: <?php // 用戶管理操作 不是界面 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php;','root','root'); $action = strtolower(trim($_GET['action'])); switch($action){ case 'save': // 更新操作 $stmt = $pdo->prepare("UPDATE `staff` SET `email` = :email,`password` = :password WHERE id = :id"); $email = strtolower(trim($_POST['email'])); $password = sha1(strtolower(trim($_POST['password']))); $id = $id = intval(trim($_POST['id'])); $stmt->bindParam('email',$email,PDO::PARAM_STR,60); $stmt->bindParam('password',$password,PDO::PARAM_STR,20); $stmt->bindParam('id',$id,PDO::PARAM_INT); if(true === $stmt->execute()){ // 更新成功 if($stmt->rowCount() === 1){ $status = 1; $message = '更新成功'; }else if($stmt->rowCount() === 0){ $status = 0; $message = '沒有更新'; } }else{ // 上線后 下面這句話 應(yīng)刪除 // die(print_r($stmt->errorInfo())); //開發(fā)時使用 $status = -1; $message = '保存失敗,請檢查'; } echo json_encode(['status'=>$status,'message'=>$message]); // 將信息返回給前臺 exit; breack; } $pdo = null; ?>
代碼效果圖:
首頁
員工信息
編輯頁面
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號