温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用PHP怎么实现数据库的增删查改功能

发布时间:2021-05-22 16:22:25 来源:亿速云 阅读:161 作者:Leah 栏目:开发技术

这篇文章将为大家详细讲解有关使用PHP怎么实现数据库的增删查改功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

TP_3.2.2/Application/Home/Controller/StuController.class.php

<?php 

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> 
 <h3>学生信息展示</h3> 
<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>

php有什么特点

1、执行速度快。2、具有很好的开放性和可扩展性。3、PHP支持多种主流与非主流的数据库。4、面向对象编程:PHP提供了类和对象。5、版本更新速度快。6、具有丰富的功能。7、可伸缩性。8、功能全面,包括图形处理、编码与解码、压缩文件处理、xml解析等。

关于使用PHP怎么实现数据库的增删查改功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI