Maison  >  Article  >  développement back-end  >  Explication détaillée des étapes pour implémenter l'ajout, la suppression, l'interrogation et la modification d'une base de données en PHP

Explication détaillée des étapes pour implémenter l'ajout, la suppression, l'interrogation et la modification d'une base de données en PHP

php中世界最好的语言
php中世界最好的语言original
2018-05-16 15:50:222285parcourir

Cette fois, je vais vous apporter une explication détaillée des étapes pour implémenter les ajouts, suppressions, vérifications et modifications de bases de données en PHP. Quelles sont les précautions pour implémenter les ajouts, suppressions, vérifications et modifications de bases de données en PHP. . Voici des cas pratiques, jetons un oeil.

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(&#39;stu&#39;); 
  $data[&#39;id&#39;]=&#39;&#39;; 
  $data[&#39;name&#39;]=I(&#39;get.name&#39;); 
  $data[&#39;age&#39;]=I(&#39;get.age&#39;); 
  $data[&#39;num&#39;]=I(&#39;get.num&#39;); 
  $data[&#39;address&#39;]=I(&#39;get.add&#39;); 
  $Studata->add($data); 
  $this->success("正在。。。",U(&#39;Stu/showdata&#39;)); 
 } 
 public function showdata() 
 { 
  $Studata = M(&#39;stu&#39;); 
  $data=$Studata->select(); 
  $this->assign(&#39;info&#39;,$data); 
  $this->display(&#39;school/showdata&#39;); 
 } 
 public function del(){ 
  $id = I(&#39;get.id&#39;); 
  $Studata = M(&#39;stu&#39;); 
  $bool = $Studata->where([&#39;id&#39;=>$id])->delete(); 
  if($bool){ 
   echo 1; 
  }else{ 
   echo 0; 
  } 
 } 
 public function updata() 
 { 
  $id = I(&#39;get.id&#39;); 
  $Studata = M(&#39;stu&#39;); 
  $data = $Studata->where([&#39;id&#39;=>$id])->find(); 
  $this->assign(&#39;data&#39;,$data); 
  $this->display("school/upshowdata"); 
 } 
 public function updatadeal() 
 { 
  $Studata = M(&#39;stu&#39;); 
  $id = I(&#39;get.id&#39;); 
  $data[&#39;name&#39;]=I(&#39;get.name&#39;); 
  $data[&#39;age&#39;]=I(&#39;get.age&#39;); 
  $data[&#39;num&#39;]=I(&#39;get.num&#39;); 
  $data[&#39;address&#39;]=I(&#39;get.add&#39;); 
  $bool = $Studata->where([&#39;id&#39;=>$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[&#39;id&#39;]}</td> 
  <td>{$vo[&#39;name&#39;]}</td> 
  <td>{$vo[&#39;age&#39;]}</td> 
  <td>{$vo[&#39;num&#39;]}</td> 
  <td>{$vo[&#39;address&#39;]}</td> 
  <td><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" class="del" where="{$vo[&#39;id&#39;]}">删除</a></td> 
   <td><a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" class="up" where="{$vo[&#39;id&#39;]}">修改</a></td> 
 </tr> 
</foreach> 
</table> 
</center> 
</body> 
</html> 
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script> 
<script> 
 $(&#39;.del&#39;).click(function () { 
  var where = $(this).attr(&#39;where&#39;); 
  $.ajax({ 
   type: "get", 
   url: "{:U(&#39;Stu/del&#39;)}?id="+where, 
   success: function(msg){ 
    if(msg==1){ 
     alert(&#39;删除成功&#39;); 
     location.href(&#39;showdata&#39;); 
    }else { 
     alert(&#39;删除失败&#39;); 
    } 
   } 
  }); 
 }) 
 $(&#39;.up&#39;).click(function () { 
  var where = $(this).attr(&#39;where&#39;); 
  location.href(&#39;updata?id=&#39;+where); 
  // $.ajax({ 
  //  type: "get", 
  //  url: "{:U(&#39;Stu/updata&#39;)}?id="+where, 
  //  success: function(msg){ 
  //   $(&#39;#content&#39;).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(&#39;Stu/getdata&#39;)}" 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(&#39;Stu/updatadeal&#39;)}" method="get"> 
 <input type="hidden" value="{$data[&#39;id&#39;]}" name="id"> 
 <br> 
 名字: <input type="text" name="name" value="{$data[&#39;name&#39;]}"> 
 <br> 
 年龄: <input type="text" name="age" value="{$data[&#39;age&#39;]}"> 
 <br> 
 学号:<input type="text" name="num" value="{$data[&#39;num&#39;]}"> 
 <br> 
 籍贯:<input type="text" name="add" value="{$data[&#39;address&#39;]}"> 
 <br> 
 <input type="submit" value="提交"> 
 <br> 
</form> 
</body> 
</html>

Pour exécuter, saisissez dans le navigateur : http://127.0.0.1:90/TP_3.2.2/index. php /Accueil/Stu/stushow

Cliquez pour supprimer

Modifier :

Je pense que vous maîtrisez la méthode après avoir lu le cas dans cet article. Pour des informations plus intéressantes, veuillez prêter attention aux autres articles connexes sur le php chinois. site web!

Lecture recommandée :

Explication détaillée de l'utilisation du tri à bulles PHP

Explication détaillée des étapes pour implémenter Huffman encodage/décodage en PHP

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn