TP5.0框架中简单CURD操作

本文介绍了如何在ThinkPHP5.0框架中进行基本的CRUD操作,包括创建model和view目录,配置数据库,定义User模型,以及在控制器中实现增删查改的方法。通过示例代码展示了如何进行用户数据的插入、查询、删除和更新,同时提供了相应的视图文件用于展示和交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:在浏览器中运行框架,完毕后。在application/index中新建model目录和view目录(view目录里并建index目录)。然后在application里的database.php里配置数据库名 用户名 密码。

第二步:在model目录里新建User.php模型层.

User.php中内容为:

<?php
namespace app\index\model;



use think\Db;//引入Db
use think\Model;//引入Model



class User extends Model
{

protected $table='user';//表名


//增加
function insertData($data)
{

return Db::table($this->table)->insertGetId($data);
}
//查询
function show()
{
return Db::table($this->table)->select();    
}
//删除
function deleteData($id)
{
return Db::table($this->table)->where('uid','=',$id)->delete();    
}
//查询单条
function findData($id)
{
return Db::table($this->table)->where('uid','=',$id)->find();    
}
//修改
function updateData($data,$id)
{
return Db::table($this->table)->where('uid','=',$id)->update($data);    
}
}
?>

第三步:controller目录里控制器index.php里面内容为:

<?php
namespace app\index\controller;


use think\controller;//引入控制器
use think\Request;//引入request
use app\index\model\User;//引入模型层
header("content-type:text/html;charset=UTF-8");//防乱码
class Index
{
    //表单页面
    public function index()
    {
        //echo 123;
        //return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>';
        return view();
    }
    //添加
    public function insert()
    {
        
        $Request=Request::instance();//调用Request中instance方法
        $data=$Request->post();//接值
        //print_r($data);
        $user=new User;//实例化model
        $result=$user->insertData($data);//执行添加语句
        if($result)
        {   
            return Redirect('Index/show');
            //echo "<script>alert('新增成功');localtion.href='{:url('Index/show')}'</script>";
            //$this->success('新增成功','Index/show');
        }else{
            $this->error('新增失败');
        }

    }
   //展示
   public function show()
   {
     $user=new User;//实例化model
     $arr=$user->show();//执行查询
     return view('show',['arr'=>$arr]);
   }
   //删除
   public function delete()
   {
   $Request=Request::instance();
   $id=$Request->get('id');
   $user=new User;
   $result=$user->deleteData($id);
   if($result)
   {
    return Redirect('Index/show');
   }else{
    $this->error('删除失败');
   }
   }
   //修改页面
   public function update()
   {
    $Request=Request::instance();
    $id=$Request->get('id');
    $user=new User;
    $res=$user->findData($id);//查询单条
    //print_r($res);die;
    return view('update',['res'=>$res]);
   }
   //执行修改
   public function save()
   {
    $Request=Request::instance();
    $id=$Request->post('uid');
    $Request=Request::instance();
    $data=$Request->post();//接修改之后的数据
    $user=new User;
    $result=$user->updateData($data,$id);//修该语句
    if($result)
    {
    return Redirect('Index/show');
    }else{
        $this->error('修改失败');
    }


   }

}

 第四步:在视图层index目录里新建index.html show.html update.html里面内容分别为:

index.html中:

<!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" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>用户注册</title>
</head>
<body>
<center>
<form action="{:url('Index/insert')}" method="post">
    <table>
        <tr>
            <td>用户名</td>
            <td><input type="text" name="username"/></td>
        </tr>
        <tr>
            <td>密码</td>
            <td><input type="password" name="password" /></td>
        </tr>
        <tr>
            <td><input type="submit" value="提交" /></td>
            <td></td>
        </tr>
    </table>
</form>
</center>    
</body>
</html>

show.html中:

<!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" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>用户列表</title>
</head>
<body>
    <center>
        <table>
            <tr>
                <td>ID</td>
                <td>用户名</td>
                <td>密码</td>
                <td>操作</td>
            </tr>
            {volist name="arr" id="vo"}
            <tr>
                <td>{$vo.uid}</td>
                <td>{$vo.username}</td>
                <td>{$vo.password}</td>
                <td><a href="delete?id={$vo.uid}">删除</a>||<a href="update?id={$vo.uid}">修改</a></td>
            </tr>
            {/volist}
        </table>
    </center>
</body>
</html>

update.html中:

<!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" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>修改信息</title>
</head>
<body>
 <center>
 
     <form action="{:url('Index/save')}" method="post">
     <input type="hidden" name="uid" value="{$res['uid']}"/>
         
        <table>
           <!--  <tr>
                <td></td>
                <td><input type="text" name="uid" value="{$res['uid']}"/></td>
            </tr>  -->  
             <tr>
                 <td>用户名</td>
                 <td><input type="text" name="username" value="{$res['username']}"/></td>
             </tr>
             <tr>
                 <td>密码</td>
                 <td><input type="password" name="password" value="{$res['password']}" /></td>
             </tr>
             <tr>
                 <td><input type="submit" value="修改" /></td>
                 <td></td>
             </tr>
         </table>
     </form>
 </center>
</body>
</html>

注意:TP5.0中路由配置规则是在route.php中写入return['hello/[:name]'=>'index/hello',]//其中index是控制器名,hello是方法名

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值