PHP實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪查改功能及完整代碼
本文用到: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>數(shù)據(jù)展示界面</title>
</head>
<body id="content">
<center>
<h2>學(xué)生信息展示</h2>
<table border="1">
<th>編號(hào)</th>
<th>姓名</th>
<th>年齡</th>
<th>學(xué)號(hào)</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)" rel="external nofollow" rel="external nofollow" class="del" where="{$vo['id']}">刪除</a></td>
<td><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" 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> 學(xué)號(hào)注冊(cè)查詢(xún)系統(tǒng) </title>
</head>
<body>
<form action="{:U('Stu/getdata')}" method="get">
<br>
名字: <input type="text" name="name">
<br>
年齡: <input type="text" name="age">
<br>
學(xué)號(hào):<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> 學(xué)號(hào)注冊(cè)查詢(xún)系統(tǒng) </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>
學(xué)號(hào):<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>
執(zhí)行在瀏覽器里面輸入:http://127.0.0.1:90/TP_3.2.2/index.php/Home/Stu/stushow
點(diǎn)擊刪除



修改:


總結(jié)
以上所述是小編給大家介紹的PHP實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪查改功能及完整代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
thinkPHP分組后模板無(wú)法加載問(wèn)題解決方法
這篇文章主要介紹了thinkPHP分組后模板無(wú)法加載問(wèn)題解決方法,分析了thinkPHP分組后模板無(wú)法加載的原因與相應(yīng)的設(shè)置方法,需要的朋友可以參考下2016-07-07
PHP將數(shù)據(jù)導(dǎo)出Excel表中的實(shí)例(投機(jī)型)
下面小編就為大家?guī)?lái)一篇PHP將數(shù)據(jù)導(dǎo)出Excel表中的實(shí)例(投機(jī)型)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
微信公眾平臺(tái)開(kāi)發(fā)實(shí)現(xiàn)2048游戲的方法
這篇文章主要介紹了微信公眾平臺(tái)開(kāi)發(fā)實(shí)現(xiàn)2048游戲的方法,較為詳細(xì)的講述的2048游戲的原理以及微信公眾平臺(tái)開(kāi)發(fā)2048游戲的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
yii使用activeFileField控件實(shí)現(xiàn)上傳文件與圖片的方法
這篇文章主要介紹了yii使用activeFileField控件實(shí)現(xiàn)上傳文件與圖片的方法,較為詳細(xì)的分析了activeFileField控件用于文件傳輸?shù)木唧w使用技巧,需要的朋友可以參考下2015-12-12
一個(gè)PHP并發(fā)訪(fǎng)問(wèn)實(shí)例代碼
php其實(shí)也可以執(zhí)行多任務(wù)或并發(fā)訪(fǎng)問(wèn),下面就是網(wǎng)上找來(lái)的一個(gè)關(guān)于PHP并發(fā)訪(fǎng)問(wèn)的例子2012-09-09

