search
HomeBackend DevelopmentPHP TutorialThinkPHP implements user information query, update and delete functions

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

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>

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!

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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

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

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software