abstrak:填加數(shù)據(jù):insert.php <?php /** * 添加數(shù)據(jù) */ //引入smarty配置 include __DIR__.'/config/config.php'; //連接數(shù)據(jù)庫 require __DIR__.'/medoo/conn.php'; @$act = $_R
填加數(shù)據(jù):insert.php <?php /** * 添加數(shù)據(jù) */ //引入smarty配置 include __DIR__.'/config/config.php'; //連接數(shù)據(jù)庫 require __DIR__.'/medoo/conn.php'; @$act = $_REQUEST['act']; if($act=='get') { $name = $_REQUEST['name']; $age = $_REQUEST['age']; $sex = $_REQUEST['sex']; $email = $_REQUEST['email']; $password = $_REQUEST['password']; $zt = $_REQUEST['status']; $time = $_REQUEST['create_time']; //插入數(shù)據(jù)操作 $table = 'user'; $data['name'] = $name; $data['age'] = $age; $data['sex'] = $sex; $data['email'] = $email; $data['password'] = sha1("$password"); $data['status'] = $zt; $data['create_time'] = $time; //查詢用戶名是否存在 $res = $db->select($table,'name');//查詢數(shù)據(jù)庫所有用戶名 $isin = in_array($name,$res); //在查詢結(jié)果中查找用戶名是否存在 if ($isin){ echo "<script>alert('您添加的用戶名已經(jīng)存在!');location.href='insert.php'</script>"; exit(); } //執(zhí)行插入操作 $stmt = $db->insert($table, $data); //查詢受影響的記錄數(shù)量 $num = $stmt->rowCount(); if ($num > 0) { echo "<script>alert('成功添加了{$num}條記錄');location.href='insert.php'</script>"; } } //模板渲染 $smarty->display('insert.html');
添加數(shù)據(jù)模板:insert.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加數(shù)據(jù)</title> </head> <body> {include file="head.html"} <form action="insert.php?act=get" method="post"> <input type="hidden" name="create_time" value="{$smarty.now}"> <table border="1" cellpadding="5" cellspacing="0" width="60%"> <caption> <h2>添加用戶</h2> </caption> <tr> <td width="100"> 用戶名:</td> <td><input name="name" value="" type="text"></td> </tr> <tr> <td> 密碼:</td> <td><input name="password" value="" type="password"></td> </tr> <tr> <td> 年齡:</td> <td><input name="age" value="" type="text"></td> </tr> <tr> <td> 性別:</td> <td><input name="sex" type="radio" value="0" checked /> 男 <input name="sex" type="radio" value="1" /> 女</td> </tr> <tr> <td> 電子郵箱:</td> <td><input name="email" type="text" value=""></td> </tr> <tr> <td> 用戶狀態(tài):</td> <td><input name="status" type="radio" value="1" checked /> 啟用 <input name="status" type="radio" value="0" /> 禁用</td> </tr> <tr> <td></td> <td><input type="submit" value="提交"> </td> </tr> </table> </form> </body> </html>
查詢數(shù)據(jù):search.php <?php /** * 查詢數(shù)據(jù) */ //引入smarty配置 include __DIR__.'/config/config.php'; //連接數(shù)據(jù)庫 require __DIR__.'/medoo/conn.php'; $rows =[]; @$act = $_REQUEST['act']; if($act=='so') { //設(shè)置表名 要查詢的數(shù)據(jù) $table = 'user'; $data = ['user_id','name','sex','age','email','create_time']; //接收表單傳過來的數(shù)據(jù) $sel = $_REQUEST['sel']; $name = $_REQUEST['name']; $age = $_REQUEST['age']; @$sex = $_REQUEST['sex']; //性別不為空 if ($sex !='') { //性別不為空 姓名不為空 if ($name != '') { //性別不為空 姓名不為空 年齡不為空 if ($age != '') { if ($sel == '='){ $where = ['AND' => ['name' => $name, 'age' => $age, 'sex' => $sex]]; }elseif ($sel == '>'){ $where = ['AND' => ['name' => $name, 'age[>]' => $age, 'sex' => $sex]]; }else{ $where = ['AND' => ['name' => $name, 'age[<]' => $age, 'sex' => $sex]]; } //性別不為空 姓名不為空 年齡為空 }else { $where = ['AND' => ['name' => $name, 'sex' => $sex]]; } //性別不為空 姓名為空 } else { //性別不為空 姓名為空 年齡不為空 if ($age != '') { if ($sel == '=') { $where = ['AND' => ['age' => $age,'sex'=>$sex]]; } elseif ($sel == '>') { $where = ['AND' => ['age[>]' => $age,'sex'=>$sex]]; } else { $where = ['AND' => ['age[<]' => $age,'sex'=>$sex]]; } //性別不為空 姓名為空 年齡為空 } else { $where = ['sex'=>$sex]; } } //性別為空 }else { //性別為空 姓名不為空 if ($name != '') { //性別為空 姓名不為空 年齡不為空 if ($age != '') { if ($sel == '=') { $where = ['AND' => ['age' => $age,'name'=>$name]]; } elseif ($sel == '>') { $where = ['AND' => ['age[>]' => $age,'name'=>$name]]; } else { $where = ['AND' => ['age[<]' => $age,'name'=>$name]]; } //性別為空 姓名不為空 年齡為空 } else { $where = ['name'=>$name]; } //性別為空 姓名為空 }else{ //性別為空 姓名為空 年齡不為空 if ($age != '') { if ($sel == '=') { $where = ['age' => $age]; } elseif ($sel == '>') { $where = ['age[>]' => $age]; } else { $where = ['age[<]' => $age]; } //性別為空 姓名為空 年齡為空 } else { $where = ''; } } } // 執(zhí)行查詢操作 $res = $db->select($table,$data,$where); //遍歷結(jié)果集 foreach ($res as $row){ $rows[] = $row; } //進行模板變量替換 $smarty->assign('rows',$rows); } $smarty->assign('act',$act); //模板渲染 $smarty->display('search.html');
查詢數(shù)據(jù)模板:search.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>查詢數(shù)據(jù)</title> </head> <body> {include file="head.html"} <form action="search.php?act=so" method="post"> <table border="1" cellpadding="5" cellspacing="0" align="center" width="60%"> <caption> <h2>用戶查詢</h2> </caption> <tr> <td width="100">用戶名:</td> <td><input type="text" name="name" value=""> </td> </tr> <tr> <td>性別:</td> <td><input name="sex" type="radio" value="0" />男 <input name="sex" type="radio" value="1" />女 </td> </tr> <tr> <td>年齡:</td> <td><select name="sel"> <option value="=">=</option> <option value=">">></option> <option value="<"><</option> </select> <input name="age" type="text" value="" /> </td> </tr> <tr> <td></td> <td><input type="submit" value="提交"> <input type="reset" value="清除所有條件"> </td> </tr> </table> </form> <br><br> {if $act eq 'so'} <table border="1" cellpadding="5" cellspacing="0" align="center" width="60%"> <caption><h2>用戶信息表</h2></caption> <tr bgcolor="#90ee90"> <th>ID</th> <th>姓名</th> <th>性別</th> <th>年齡</th> <th>郵箱</th> <th>入職時間</th> </tr> {foreach $rows as $row} <tr align="center"> <td>{$row.user_id}</td> <td>{$row.name}</td> <td>{if $row.sex eq '0'} 男 {else} 女 {/if} </td> <td>{$row.age}</td> <td>{$row.email}</td> <td>{$row.create_time|date_format:"%Y/%m/%d %H:%M:%S "}</td> </tr> {foreachelse} <tr align="center"><td colspan="6"><h3>沒有符合條件的數(shù)據(jù)</h3></td></tr> {/foreach} </table> {/if} </body> </html>
更改數(shù)據(jù):update.php <?php /** * 更新操作 */ //引入smarty配置 include __DIR__.'/config/config.php'; //連接數(shù)據(jù)庫 require __DIR__.'/medoo/conn.php'; @$act=$_REQUEST['act']; if ($act=='update'){ //接收表單提交的值 $sel = $_REQUEST['sel']; $name = $_REQUEST['name']; @$sex = $_REQUEST['sex']; $age = $_REQUEST['age']; $email= $_REQUEST['email']; //要更新的數(shù)據(jù) $table='user'; //判斷用戶名是否為空 if ($name ==''){ echo "<script>alert('請?zhí)顚懹脩裘?#39;);location.href='update.php'</script>"; exit(); //判斷用戶名是否在數(shù)據(jù)庫有對應(yīng)記錄 }else{ $res = $db->select($table,'name');//查詢數(shù)據(jù)庫所有用戶名 $isin = in_array($name,$res); //在查詢結(jié)果中查找用戶名是否存在 if (!$isin){ echo "<script>alert('您要更新的用戶名不存在!請重試!');location.href='update.php'</script>"; exit(); } } //性別不為空 if ($sex !=''){ //性別不為空 年齡不為空 if ($age !=''){ //性別不為空 年齡不為空 郵箱不為空 if ($email !=''){ if ($sel =='+'){ $data['email'] = $email; $data['age[+]'] = $age; $data['sex'] = $sex; }else{ $data['email'] = $email; $data['age'] = $age; $data['sex'] = $sex; } //性別不為空 年齡不為空 郵箱為空 }else{ if ($sel =='+'){ $data['age[+]'] = $age; $data['sex'] = $sex; }else{ $data['age'] = $age; $data['sex'] = $sex; } } //性別不為空 年齡為空 }else{ //性別不為空 年齡為空 郵箱不為空 if ($email !=''){ $data['email'] = $email; $data['sex'] = $sex; //性別不為空 年齡為空 郵箱為空 }else{ $data['sex'] = $sex; } } //性別為空 }else{ //性別為空 年齡不為空 if ($age !=''){ //性別為空 年齡不為空 郵箱不為空 if ($email !=''){ if ($sel =='+'){ $data['email'] = $email; $data['age[+]'] = $age; }else{ $data['email'] = $email; $data['age'] = $age; } //性別為空 年齡不為空 郵箱為空 }else{ if ($sel =='+'){ $data['age[+]'] = $age; }else{ $data['age'] = $age; } } //性別為空 年齡為空 }else{ //性別為空 年齡為空 郵箱不為空 if ($email !=''){ $data['email'] = $email; //性別為空 年齡為空 郵箱為空 }else{ echo "<script>alert('請?zhí)顚懜滦畔ⅲ?#39;);location.href='update.php'</script>"; exit(); } } } //更新條件 $where['name'] =$name; //執(zhí)行更新操作 $stmt = $db->update($table,$data,$where); $num = $stmt->rowCount(); if ($num>0){ echo "<script>alert('成功更新了{$num}條記錄');location.href='update.php'</script>"; } } //模板渲染 $smarty->display('update.html');
更改數(shù)據(jù)模板:update.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>更新數(shù)據(jù)</title> </head> <body> {include file="head.html"} <form action="update.php?act=update" method="post"> <table border="1" cellpadding="5" cellspacing="0" align="center" width="60%"> <caption> <h2>用戶信息修改</h2> </caption> <tr> <td width="100">用戶名:<br>(條件,必填)</td> <td><input type="text" name="name" value=""> </td> </tr> <tr> <td>性別:</td> <td><input name="sex" type="radio" value="0" />男 <input name="sex" type="radio" value="1" />女 </td> </tr> <tr> <td>年齡:</td> <td><select name="sel"> <option value="">=</option> <option value="+">+</option> </select> <input name="age" type="text" value="" /> </td> </tr> <tr> <td>郵箱:</td> <td><input name="email" type="text" value="" /> </td> </tr> <tr> <td></td> <td><input type="submit" value="提交"> <input type="reset" value="清除所有條件"> </td> </tr> </table> </form> </body> </html>
刪除數(shù)據(jù):delete.php <?php /** * 刪除數(shù)據(jù) */ //引入smarty配置 include __DIR__.'/config/config.php'; //連接數(shù)據(jù)庫 require __DIR__.'/medoo/conn.php'; @$act=$_REQUEST['act']; if ($act=='del') { //接收表單傳過來的數(shù)據(jù) $id = $_REQUEST['id']; $aqm = $_REQUEST['aqm']; //驗證安全碼 if ($aqm !='php'){ echo "<script>alert(' 安全碼不正確!請重試!');location.href='delete.php'</script>"; exit(); } //設(shè)置表名 $table = 'user'; $where['user_id'] = $id; //判斷id是否為空 if ($id ==''){ echo "<script>alert('請?zhí)顚懹脩鬒D!');location.href='delete.php'</script>"; exit(); //判斷id是否在數(shù)據(jù)庫有對應(yīng)記錄 }else{ $res = $db->select($table,'user_id');//查詢數(shù)據(jù)庫所有用戶ID $isin = in_array($id,$res); //在查詢結(jié)果中查找用戶ID是否存在 if (!$isin){ echo "<script>alert('您要刪除的用戶ID不存在!請重試!');location.href='delete.php'</script>"; exit(); } } //執(zhí)行刪除操作 $stmt= $db->delete($table,$where); $num=$stmt->rowCount(); if ($num>0){ echo "<script>alert('成功刪除了{$num}條記錄');location.href='delete.php'</script>"; } } $smarty->display("delete.html");
刪除數(shù)據(jù)模板:delete.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>刪除用戶</title> </head> <body> {include file="head.html"} <form action="delete.php?act=del" method="post"> <table border="1" cellpadding="5" cellspacing="0" align="center" width="60%"> <caption> <h2>用戶刪除</h2> </caption> <tr> <td width="100">用戶id:<br>(條件,必填)</td> <td><input type="text" name="id" value=""> </td> </tr> <tr> <td width="100">安全碼:</td> <td><input type="text" name="aqm" value="">(為避免誤操作,安全碼為必填,安全碼默認(rèn)為:php) </td> </tr> <tr> <td></td> <td><input type="submit" value="提交"> </td> </tr> </table> </form> </body> </html>
里面用了大量的if else嵌套,是否有更簡單高效的處理函數(shù),請老師指點,謝謝, 演示地址:http://demo.lidiy.cn
Guru membetulkan:韋小寶Masa pembetulan:2019-02-22 11:53:00
Rumusan guru:不錯不錯 寫的很棒 代碼也很完整!!