Home > Backend Development > PHP Tutorial > ThinkPHP implements user information query, update and delete functions

ThinkPHP implements user information query, update and delete functions

小云云
Release: 2023-03-22 09:34:01
Original
2276 people have browsed it

This article mainly introduces the ThinkPHP framework to implement user information query, update and delete functions, and analyzes the database configuration, control and template calling of thinkPHP framework to implement information query, update, delete and other functions in the form of examples. Friends who need it You can refer to it, I hope it can help everyone.

One code

1. Configuration file


<?php
return array(
  &#39;APP_DEBUG&#39; => false,    // 关闭调试模式
  &#39;DB_TYPE&#39;=> &#39;mysql&#39;,    // 数据库类型
  &#39;DB_HOST&#39;=> &#39;localhost&#39;,   // 数据库服务器地址
  &#39;DB_NAME&#39;=>&#39;db_database30&#39;,     // 数据库名称
  &#39;DB_USER&#39;=>&#39;root&#39;,      // 数据库用户名
  &#39;DB_PWD&#39;=>&#39;root&#39;,        // 数据库密码
  &#39;DB_PORT&#39;=>&#39;3306&#39;,      // 数据库端口
  &#39;DB_PREFIX&#39;=>&#39;think_&#39;,    // 数据表前缀
);
?>
Copy after login

2. Entry file


<?php
define(&#39;THINK_PATH&#39;, &#39;../ThinkPHP&#39;);    //定义ThinkPHP框架路径(相对于入口文件)
define(&#39;APP_NAME&#39;, &#39;App&#39;);       //定义项目名称
define(&#39;APP_PATH&#39;, &#39;./App&#39;);        //定义项目路径
require(THINK_PATH."/ThinkPHP.php");  //加载框架入口文件
App::run();               //实例化一个网站应用实例
?>
Copy after login

3. Controller file


<?php
header("Content-Type:text/html; charset=utf-8");  //设置页面编码格式
class IndexAction extends Action{
  public function index(){
    $db = M(&#39;User&#39;);              // 实例化模型类,参数数据表名称,不包含前缀
    $select = $db->order(&#39;id desc&#39;)->limit(10)->select();
    $this->assign(&#39;select&#39;,$select);       // 模板变量赋值
    $this->display();              // 指定模板页
  }
  public function update(){
    $db = M(&#39;User&#39;);              // 实例化模型类,参数数据表名称,不包含前缀
    $select = $db->where(&#39;id=&#39;.$_GET[&#39;id&#39;])->select();
    $this->assign(&#39;select&#39;,$select);       // 模板变量赋值
    $this->display(update);             // 指定模板页
    if(isset($_POST[&#39;id&#39;])){
      $data[&#39;user&#39;] = $_POST[&#39;user&#39;];       // 要修改的数据对象属性赋值
      $data[&#39;pass&#39;] = md5($_POST[&#39;pass&#39;]);
      $data[&#39;address&#39;] = $_POST[&#39;address&#39;];
      $result=$db->where(&#39;id=&#39;.$_POST[&#39;id&#39;])->save($data);   // 根据条件保存修改的数据
      if($result){
        $this->redirect(&#39;Index/index&#39;,&#39;&#39;, 2,&#39;数据更新成功&#39;);    //页面重定向
      }
    }
  }
  public function delete(){
    $db = M(&#39;User&#39;);              // 实例化模型类,参数数据表名称,不包含前缀
    $result=$db->where(&#39;id=&#39;.$_GET[&#39;id&#39;])->delete();   // 删除id为5的用户数据
    if($result){
      $this->redirect(&#39;Index/index&#39;,&#39;&#39;, 2,&#39;数据删除成功&#39;);    //页面重定向
    }
  }
}
?>
Copy after login

4. Template file one


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户信息输出</title>
<link href="__ROOT__/Public/Css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">
 <tr>
  <td colspan="4" bgcolor="#FFFFFF" class="title" align="center">用户信息</td>
 </tr>
 <tr class="title">
  <td bgcolor="#FFFFFF" width="44">ID</td>
  <td bgcolor="#FFFFFF" width="120">名称</td>
  <td bgcolor="#FFFFFF" width="111">地址</td>
  <td bgcolor="#FFFFFF" width="111">操作</td>
 </tr>
 <foreach name=&#39;select&#39; item=&#39;user&#39; >
 <tr class="content">
  <td bgcolor="#FFFFFF">{$user.id}</td>
  <td bgcolor="#FFFFFF">{$user.user}</td>
  <td bgcolor="#FFFFFF">{$user.address}</td>
  <td bgcolor="#FFFFFF"><a href="__URL__/update?id={$user.id}" rel="external nofollow" >更新</a>/<a href="__URL__/delete?id={$user.id}" rel="external nofollow" >删除</a></td>
 </tr>
 </foreach>
</table>
</body>
</html>
Copy after login

5. Template file two


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户信息输出</title>
<link href="__ROOT__/Public/Css/style.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form2" name="form2" method="post" action="__URL__/update">
<table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">
 <tr>
  <td colspan="2" bgcolor="#FFFFFF" class="title" align="center">用户信息</td>
 </tr>
 <foreach name=&#39;select&#39; item=&#39;user&#39; >
 <tr class="content">
  <td bgcolor="#FFFFFF" class="right" width="103">名称:</td>
  <td bgcolor="#FFFFFF" width="289"> <input type="hidden" name="id" id="hiddenField" value="{$user.id}" /><input name="user" type="text" id="user" size="20" value="{$user.user}" /></td>
  </tr>
 <tr class="content">
  <td bgcolor="#FFFFFF" class="right">密码:</td>
  <td bgcolor="#FFFFFF"><input name="pass" type="password" id="pass" size="20" value="{$user.pass}" />
   </td>
  </tr>
 <tr class="content">
  <td bgcolor="#FFFFFF" class="right"> 地址:</td>
  <td bgcolor="#FFFFFF"> 
   <input name="address" type="text" id="address" size="30" value="{$user.address}" />
  </td>
  </tr>
 <tr class="content">
  <td bgcolor="#FFFFFF"> </td>
  <td bgcolor="#FFFFFF"><input type="submit" name="button" id="button" value="更新" /></td>
 </tr>
 </foreach>
</table>
</form>
</body>
</html>
Copy after login

Two running results

Related recommendations:

Oracle user information query operation statement


##

The above is the detailed content of ThinkPHP implements user information query, update and delete functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template