search
HomeBackend DevelopmentPHP TutorialUse ThinkPHP framework to implement user information query, update and delete functions

Use ThinkPHP framework to implement user information query, update and delete functions

Jun 08, 2018 am 10:53 AM
thinkphp frameworkdeleterenewInquireUser Info

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 in need You can refer to the following example

This article describes how the ThinkPHP framework implements user information query, update and delete functions. Share it with everyone for your reference, the details are as follows:

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;,    // 数据表前缀
);
?>

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();               //实例化一个网站应用实例
?>

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;);    //页面重定向
    }
  }
}
?>

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>

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>

Second running results

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

ThinkPHP query statements and usage of related queries

ThinkPHP3.2.3 verification code display and Refresh and verify

Query language commonly used in ThinkPHP

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

Statement
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
Dependency Injection in PHP: Avoiding Common PitfallsDependency Injection in PHP: Avoiding Common PitfallsMay 16, 2025 am 12:17 AM

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

How to Speed Up Your PHP Website: Performance TuningHow to Speed Up Your PHP Website: Performance TuningMay 16, 2025 am 12:12 AM

ToimproveyourPHPwebsite'sperformance,usethesestrategies:1)ImplementopcodecachingwithOPcachetospeedupscriptinterpretation.2)Optimizedatabasequeriesbyselectingonlynecessaryfields.3)UsecachingsystemslikeRedisorMemcachedtoreducedatabaseload.4)Applyasynch

Sending Mass Emails with PHP: Is it Possible?Sending Mass Emails with PHP: Is it Possible?May 16, 2025 am 12:10 AM

Yes,itispossibletosendmassemailswithPHP.1)UselibrarieslikePHPMailerorSwiftMailerforefficientemailsending.2)Implementdelaysbetweenemailstoavoidspamflags.3)Personalizeemailsusingdynamiccontenttoimproveengagement.4)UsequeuesystemslikeRabbitMQorRedisforb

What is the purpose of Dependency Injection in PHP?What is the purpose of Dependency Injection in PHP?May 16, 2025 am 12:10 AM

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

How to send an email using PHP?How to send an email using PHP?May 16, 2025 am 12:03 AM

The best ways to send emails using PHP include: 1. Use PHP's mail() function to basic sending; 2. Use PHPMailer library to send more complex HTML mail; 3. Use transactional mail services such as SendGrid to improve reliability and analysis capabilities. With these methods, you can ensure that emails not only reach the inbox, but also attract recipients.

How to calculate the total number of elements in a PHP multidimensional array?How to calculate the total number of elements in a PHP multidimensional array?May 15, 2025 pm 09:00 PM

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

What are the characteristics of do-while loops in PHP?What are the characteristics of do-while loops in PHP?May 15, 2025 pm 08:57 PM

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

How to hash strings in PHP?How to hash strings in PHP?May 15, 2025 pm 08:54 PM

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool