牛骨文教育服务平台(让学习变的简单)
博文笔记

TP5.0框架中简单CURD操作

创建时间:2017-03-25 投稿人: 浏览次数:158

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

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

User.php中内容为:

<?php
namespace appindexmodel;



use thinkDb;//引入Db
use thinkModel;//引入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 appindexcontroller;


use thinkcontroller;//引入控制器
use thinkRequest;//引入request
use appindexmodelUser;//引入模型层
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是方法名

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。