Home > Backend Development > PHP Tutorial > 浅谈Thinkphp中curd应用实用要点

浅谈Thinkphp中curd应用实用要点

PHPz
Release: 2021-05-31 16:02:04
forward
1579 people have browsed it

这篇文章主要介绍了Thinkphp中的curd应用实用要点并附上了简单的示例,是篇非常不错的文章,这里推荐给大家。

浅谈Thinkphp中curd应用实用要点

代码如下:

class IndexAction extends Action {
public function index(){
header(“Content-Type:text/html; charset=utf-8″);
$user=M(‘user');
$list=$user->select();
$this->assign(‘user',$list);
$this->display();
}
Copy after login

显示所有用户 ,首页做的注册

代码如下:

form action=”__URL__/add” method=”post”>
用户名<input type=”text” name=”username”>
密码<input type=”text” name=”password”>
<input type=”submit” value=”提交”>
</form>
<volist name=”user” id=”vo”>
用户名:<input name=”username” value=”<{$vo.username}>”>
密码:<input name=”password” value=”<{$vo.password}>”>
注册IP:<input name=”cip” value=”<{$vo.cip}>”>
注册时间:<input name=”ctime” value=”<{$vo.ctime}>”>
<a href=”__URL__/del/id/<{$vo.id}>”>删除</a>
<a href=”__URL__/edit/id/<{$vo.id}>”>更新</a>
<br>
</volist>
Copy after login

然后就是我们的删除方法 很简单 思路是这样的我们获取ID删除这个ID的就可以了

代码如下:

if($user->where(‘$_GET[‘id&#39;]&#39;)->delete())
{$this->success(‘删除成功&#39;);}
这
Copy after login

样就可以了

添加用户的方法

代码如下:

$user=M(‘user&#39;);
if($user->create()){
$user->cip=get_client_ip();
$user->ctime=time();
$user->password=md5(‘password&#39;);
if($user->add($data)){
$this->success(‘用户注册成功&#39;,&#39;/admin.php/index/edit&#39;);
}else{
$this->error($user->getError());
}
}else{
$this->error(getError());
}
Copy after login

更新用户是这样的 我们根据ID选择用户 输出这个用户的信息

代码如下:

$user=M(‘user&#39;);
$id=(int)$_GET[‘id&#39;];
$user=M(‘user&#39;);
$list=$user->where(“id=$id”)->find();
$this->assign(‘list&#39;,$list);
$this->display();
Copy after login

然后更新 用户更简单了 就一个save

代码如下:

$user=M(‘user&#39;);
if($user->create()){
$user->ctime=time();
if($user->save()){
$this->success(‘更新成功&#39;);
}
}else{
$this->error(‘失败&#39;);
}
Copy after login

推荐学习:《PHP视频教程

Related labels:
source:jb51.net
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