這篇文章主要介紹了PHP實現資料庫的增刪查改 ,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
本文用到:jquery、tp架構
TP_3.2.2/Application/Home/Controller/StuController.class.php
<?php/** * Created by PhpStorm. * User: root * Date: 2018/4/17 * Time: 16:32 */namespace Home\Controller;use Think\Controller;class StuController extends Controller{ public function StuShow(){ $this->display("school/stu"); } public function getdata(){ $Studata = M('stu'); $data['id']=''; $data['name']=I('get.name'); $data['age']=I('get.age'); $data['num']=I('get.num'); $data['address']=I('get.add'); $Studata->add($data); $this->success("正在。。。",U('Stu/showdata')); } public function showdata() { $Studata = M('stu'); $data=$Studata->select(); $this->assign('info',$data); $this->display('school/showdata'); } public function del(){ $id = I('get.id'); $Studata = M('stu'); $bool = $Studata->where(['id'=>$id])->delete(); if($bool){ echo 1; }else{ echo 0; } } public function updata() { $id = I('get.id'); $Studata = M('stu'); $data = $Studata->where(['id'=>$id])->find(); $this->assign('data',$data);//傳遞資料到前端頁面 $this->display("school/upshowdata"); } public function updatadeal() { $Studata = M('stu'); $id = I('get.id'); $data['name']=I('get.name'); $data['age']=I('get.age'); $data['num']=I('get.num'); $data['address']=I('get.add'); $bool = $Studata->where(['id'=>$id])->save($data); if($bool){ $this->showdata(); }else{ echo 0; } }}
TP_3.2.2/Application/Home/View/school/showdata.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>資料展示介面</title></head><body id="content"><center> <h2>學生資訊展示</h2><table border="1"> <th>編號</th> <th>姓名</th> <th>年齡</th> <th>學號</th> <th>籍貫</th> <th>操作</th> <th>操作</th><foreach name="info" item="vo" > <tr> <td>{$vo['id']}</td> <td>{$vo['name']}</td> <td>{$vo['age']}</td> <td>{$vo['num']}</td> <td>{$vo['address']}</td> <td><a href="javascript:void(0)" class="del" where="{$vo['id']}">刪除</a></td> <td><a href="javascript:void(0)" class="up" where="{$vo['id']}">修改</a></td> </tr></foreach></table></center></body></html><script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script><script> $('.del').click(function () { var where = $(this).attr('where'); $.ajax({ type: "get", url: "{:U('Stu/del')}?id="+where, success: function(msg){ if(msg==1){ alert('刪除成功'); location.href('showdata'); }else { alert('刪除失敗'); } } }); }) $('.up').click(function () { var where = $(this).attr('where'); location.href('updata?id='+where); // $.ajax({ // type: "get", // url: "{:U('Stu/updata')}?id="+where, // success: function(msg){ // $('#content').html(msg); // } // }); })</script>
TP_3.2.2/Application/Home/View/school/stu.html
<!doctype html><html lang="en"><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></head><body><form action="{:U('Stu/getdata')}" method="get"> <br> 名字: <input type="text" name="name"> <br> 年齡: <input type="text" name="age"> <br> 學號:<input type="text" name="num"> <br> 籍貫:<input type="text" name="add"> <br> <input type="submit" value="提交"> <br></form></body></html>
TP_3.2.2/Application/Home/View/school/stu.html
<!doctype html><html lang="en"><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></head><body><form action="{:U('Stu/updatadeal')}" method="get"> <input type="hidden" value="{$data['id']}" name="id"> <br> 名字: <input type="text" name="name" value="{$data['name']}"> <br> 年齡: <input type="text" name="age" value="{$data['age']}"> <br> 學號:<input type="text" name="num" value="{$data['num']}"> <br> 籍貫:<input type="text" name="add" value="{$data['address']}"> <br> <input type="submit" value="提交"> <br></form></body></html>
執行在瀏覽器裡面輸入:http://127.0.0.1:90/TP_3.2.2/index.php/Home/Stu/stushow
注意:連接埠一般是80,由於我修改過設定檔所以是90
點擊刪除
修改: