ThinkPHP框架实现用户信息查询更新及删除功能示例
作者:chengqiuming 时间:2024-06-07 15:34:11
本文实例讲述了ThinkPHP框架实现用户信息查询更新及删除功能。分享给大家供大家参考,具体如下:
一 代码
1、配置文件
<?php
return array(
'APP_DEBUG' => false, // 关闭调试模式
'DB_TYPE'=> 'mysql', // 数据库类型
'DB_HOST'=> 'localhost', // 数据库服务器地址
'DB_NAME'=>'db_database30', // 数据库名称
'DB_USER'=>'root', // 数据库用户名
'DB_PWD'=>'root', // 数据库密码
'DB_PORT'=>'3306', // 数据库端口
'DB_PREFIX'=>'think_', // 数据表前缀
);
?>
2、入口文件
<?php
define('THINK_PATH', '../ThinkPHP'); //定义ThinkPHP框架路径(相对于入口文件)
define('APP_NAME', 'App'); //定义项目名称
define('APP_PATH', './App'); //定义项目路径
require(THINK_PATH."/ThinkPHP.php"); //加载框架入口文件
App::run(); //实例化一个网站应用实例
?>
3、控制器文件
<?php
header("Content-Type:text/html; charset=utf-8"); //设置页面编码格式
class IndexAction extends Action{
public function index(){
$db = M('User'); // 实例化模型类,参数数据表名称,不包含前缀
$select = $db->order('id desc')->limit(10)->select();
$this->assign('select',$select); // 模板变量赋值
$this->display(); // 指定模板页
}
public function update(){
$db = M('User'); // 实例化模型类,参数数据表名称,不包含前缀
$select = $db->where('id='.$_GET['id'])->select();
$this->assign('select',$select); // 模板变量赋值
$this->display(update); // 指定模板页
if(isset($_POST['id'])){
$data['user'] = $_POST['user']; // 要修改的数据对象属性赋值
$data['pass'] = md5($_POST['pass']);
$data['address'] = $_POST['address'];
$result=$db->where('id='.$_POST['id'])->save($data); // 根据条件保存修改的数据
if($result){
$this->redirect('Index/index','', 2,'数据更新成功'); //页面重定向
}
}
}
public function delete(){
$db = M('User'); // 实例化模型类,参数数据表名称,不包含前缀
$result=$db->where('id='.$_GET['id'])->delete(); // 删除id为5的用户数据
if($result){
$this->redirect('Index/index','', 2,'数据删除成功'); //页面重定向
}
}
}
?>
4、模板文件一
<!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='select' item='user' >
<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、模板文件二
<!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='select' item='user' >
<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>
二 运行结果
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/78600757
标签:ThinkPHP,查询,更新,删除
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mysql中错误:1093-You can’t specify target table for update in FROM clause的解决方法
2024-01-14 06:11:58
Linux下mysql 5.6.17 安装图文教程
2024-01-26 10:59:16
div不换行,css不换行,自动换行
2009-09-07 12:52:00
Python + selenium + crontab实现每日定时自动打卡功能
2021-06-10 19:45:42
![](https://img.aspxhome.com/file/2023/6/84926_0s.png)
python matplotlib工具栏源码探析二之添加、删除内置工具项的案例
2022-05-16 19:31:22
![](https://img.aspxhome.com/file/2023/1/63601_0s.png)
利用Psyco提升Python运行速度
2021-05-02 19:02:50
Python的条件锁与事件共享详解
2021-12-16 22:02:23
磁盘垃圾文件清理器python代码实现
2023-08-24 23:20:41
![](https://img.aspxhome.com/file/2023/9/60709_0s.jpg)
PHP面向对象程序设计之类常量用法实例
2023-11-22 15:17:39
MySQL 随机函数获取数据速度和效率分析
2024-01-13 00:43:01
模仿MSN消息提示的效果
2013-07-02 06:22:28
Python下实现的RSA加密/解密及签名/验证功能示例
2022-06-25 03:07:16
微信小程序实现视频播放器发送弹幕
2024-04-19 11:04:21
![](https://img.aspxhome.com/file/2023/3/136173_0s.jpg)
通过python读取txt文件和绘制柱形图的实现代码
2023-11-23 11:24:43
![](https://img.aspxhome.com/file/2023/1/97331_0s.png)
Python jieba库分词模式实例用法
2023-12-09 23:40:42
基于go实例网络存储协议详解
2024-04-25 13:19:30
CentOS 6、7下mysql 5.7 详细安装教程
2024-01-24 18:00:47
![](https://img.aspxhome.com/file/2023/9/121309_0s.jpg)
Pytorch如何把Tensor转化成图像可视化
2021-11-03 20:20:22
jQuery实现的简单分页示例
2024-04-09 19:45:29
Linux CentOS下docker部署Asp.Net Core(.Net6)
2024-06-05 15:44:00
![](https://img.aspxhome.com/file/2023/4/122834_0s.jpg)