//php對數據庫的封裝 //Mysql_fetach($sql)函數查詢全部的 function Mysql_fetach($sql){ $conn=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);//這些全局常量定義在config.php文件中,分別表示(loocalhost,root,password,數據庫名稱) if(!$conn){ exit('數據庫鏈接失敗'); } $query=mysqli_query($conn,$sql); if(!$query){ return false; } while($row=mysqli_fetch_assoc($query)){ $result[]=$row; }; return $result; } function show_query($sql){ $conn=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME); if(!$conn){ exit('數據庫鏈接失敗'); } $query=mysqli_query($conn,$sql); if(!$query){ return false; } return $conn; } //Mysql_fetach_once($sql)只查詢其中的一條 function Mysql_fetach_once($sql){ $res=Mysql_fetach($sql); return isset($res)?$res[0]:''; } //Mysql_execute($sql)執行增刪改操做,返回的是受影響行數 function Mysql_execute($sql){ $conn=show_query($sql); //須要返回受影響行數,對於增長刪除修改都是獲取受影響行數 $affected_rows=mysqli_affected_rows($conn); return $affected_rows; }
在服務端的頁面上引入上面的數據庫封裝函數,服務端接受查詢到的數據,再進行表單渲染便可php
//服務端接受查詢到的數據 $categories=Mysql_fetach('select * from categories;'); ...... //客戶端渲染 <?php foreach($categories as $item):?> <tr> <td class="text-center"><input type="checkbox" data-id="<?php echo $item['id'];?>"></td> <td><?php echo $item['name'];?></td> <td><?php echo $item['slug'];?></td> <td class="text-center"> <a href="/admin/categories.php?id=<?php echo $item['id']; ?>" class="btn btn-info btn-xs">編輯</a> <a href="/admin/api/dete_categori.php?id=<?php echo $item['id']; ?>" class="btn btn-danger btn-xs">刪除</a> </td> </tr> <?php endforeach?>
1)點擊「刪除」按鈕以後,執行刪除操做,因此第一步從刪除按鈕開始,爲了告知服務端咱們須要刪除的元素是哪個,咱們傳給服務端一個不一樣的idmysql
<a href="/admin/api/dete_categori.php?id=<?php echo $item['id']; ?>" class="btn btn-danger btn-xs">刪除</a>sql
2)調用封裝好的數據庫函數數據庫
<?php require_once dirname(__FILE__).'/../../functions.php'; $id=$_GET['id']; Mysql_execute("delete from categories where id in ({$id});"); header('Location: ../categories.php');
1)原理:當有<input>被選中的時候,顯示「批量刪除」按鈕,當沒有被選中的時候,"批量刪除按鈕隱藏"api
2)每次選中checkbox的時候,將選中的ID記錄下來,保存到數組中,經過?id=的方式將數組中的ID值傳入到刪除頁面,刪除頁面對接收的數據進行批量刪除數組
3)這裏在每一個input標籤中用data-的方式增長了一個自定義屬性函數
$(function($){ var tbodyInput=$('tbody input'); //選出Input標籤,這裏是一個僞數組 var $btnDelete=$('#btn_delete'); //將按鈕選擇出來 var allId=[]; //定義一個空數組,用來存選中項的ID tbodyInput.on('change',function(){ if($(this).prop('checked')){ allId.push($(this).data('id')); }else{ allId.splice(allId.indexOf('id'),1); } allId.length? $btnDelete.fadeIn():$btnDelete.fadeOut(); $btnDelete.prop('search','?id='+allId);// });
function add_category(){ if(empty($_POST['name'])||empty($_POST['slug'])){ $GLOBALS['success'] = false; $GLOBALS['message'] = "請完整填寫表單"; return; } //接受並保存 $name=$_POST['name']; $slug=$_POST['slug']; //這裏須要向數據庫裏面插入語句, $rows=Mysql_execute("insert into categories values (null,'{$slug}','{$name}');"); $GLOBALS['success'] = $rows>0; $GLOBALS['message'] = $rows<=0?'添加失敗':'添加成功'; }
1)和添加操做頁面的區別在於編輯操做請求到的是一個含有數據的表單,而添加操做請求到的是一個空表單fetch
2)點擊的時候向頁面傳入當前點擊標籤的ID,經過不一樣的ID來辨別應該編輯的是哪個input標籤ui
2)經過from表單提交上去的action=""this
if(!empty($_GET['id'])){ //客戶端經過URL地址傳遞了一個ID,意味着可互換是要來拿一個修改數據的表單 //須要拿到用戶想要修改的數據 global $current_edit_categories; $current_edit_categories=Mysql_fetach_once("select * from categories where id = ".$_GET['id']); //下面不該該是空的表單了 } function add_category(){ if(empty($_POST['name'])||empty($_POST['slug'])){ $GLOBALS['success'] = false; $GLOBALS['message'] = "請完整填寫表單"; return; } //接受並保存 $name=$_POST['name']; $slug=$_POST['slug']; //這裏須要向數據庫裏面插入語句, $rows=Mysql_execute("insert into categories values (null,'{$slug}','{$name}');"); $GLOBALS['success'] = $rows>0; $GLOBALS['message'] = $rows<=0?'添加失敗':'添加成功'; } function edit_category(){ //只有編輯 global $current_edit_categories; /*if(empty($_POST['name'])||empty($_POST['slug'])){ $GLOBALS['success'] = false; $GLOBALS['message'] = "請完整填寫表單"; return; }*/ //接受並保存,以POST請求,若是請求到的爲空,說明須要編輯 $id=$current_edit_categories['id']; $name=empty($_POST['name']) ? $current_edit_categories['name']:$_POST['name']; $current_edit_categories['name']=$name; $slug=empty($_POST['slug']) ? $current_edit_categories['slug']:$_POST['slug']; $current_edit_categories['slug']=$slug; //這裏須要向數據庫裏面插入語句, $rows=Mysql_execute("update categories set slug ='{$slug}',name='{$name}' where id={$id};"); $GLOBALS['success'] = $rows>0; $GLOBALS['message'] = $rows<=0?'修改失敗':'修改爲功'; }