主页 > 知识库 > PHP实现数据库的增删查改功能及完整代码

PHP实现数据库的增删查改功能及完整代码

热门标签:电子围栏 服务器配置 阿里云 科大讯飞语音识别系统 银行业务 Linux服务器 团购网站 Mysql连接数设置

本文用到: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)" 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> 学号注册查询系统 /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

点击删除

修改:


总结

以上所述是小编给大家介绍的PHP实现数据库的增删查改功能及完整代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:
  • PHP 获取视频时长的实例代码
  • PHP多维数组指定多字段排序的示例代码
  • Php多进程实现代码
  • PHP 计算两个特别大的整数实例代码
  • PHP生成(支持多模板)二维码海报代码
  • PHP实现Huffman编码/解码的示例代码
  • php无限级评论嵌套实现代码
  • 如何避免PHP实例代码中的一些坏代码

标签:江苏 衡水 蚌埠 大理 枣庄 萍乡 广元 衢州

巨人网络通讯声明:本文标题《PHP实现数据库的增删查改功能及完整代码》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266