ThinkPHP中简单的CURD操作

前言


 

我们通过一个简答例子来简述CURD的操作。首先看一下数据库的样子,其中id为自增行,其它是varchar

一、查询操作


 

首先,创建在Controller文件夹下创建一个User控制器,在该控制器下写我们的查询实现方法。

<?php
namespace HomeController;
use ThinkController;
class UserController extends Controller {
    public function index(){
        $user = M('User');
        $result = $user->select();
        $this->assign('data', $result);
        $this->display();
    }
}

  在index方法中我们创建了一个Model,这个Model返回的是数据库的User表。在该表上我们调用select()方法来返回表中所有的数据。数据以数组的方式进行存储的。然后把数组传递给视图,在视图中我们显示出这个表的内容。

<!DOCTYPE html>
<html>
<head>
    <title>用户表</title>
</head>
<style type="text/css">
    table{
        border: 1px solid #DCDCDC;
    }
    table td{
        border: 1px solid #DCDCDC;
    }
</style>
<body>
    <table style="margin: 0 auto;">
        <thead>
            <tr>
                <td>id</td>
                <td>用户名</td>
                <td>用户密码</td>
            </tr>
        </thead>
        <tbody>
            <volist name="data" id="row">
                <tr>
                    <td>{$row.id}</td>
                    <td>{$row.username}</td>
                    <td>{$row.userpwd}</td>
                </tr>
            </volist>
        </tbody>
    </table>
</body>
</html>

二、删除操作


 

在控制器中添加删除方法,通过前台返回的id将数据从数据库中删除

<?php
namespace HomeController;
use ThinkController;
class UserController extends Controller {
    public function index(){
        $user = M('User');
        $result = $user->select();
        $this->assign('data', $result);
        $this->display();
    }

    public function del()
    {
        $user = M('User');
        $id = $_GET['id'];
        $count = $user->delete($id);
        if($count > 0){
            $this->success("数据删除成功");
        }else{
            $this->error("数据删除失败!");
        }
    }
}

三、修改操作


 

 public function modify(){
        $user = M('User');
        $id = $_GET['id'];
        $result = $user->find($id);
        $this->assign('data', $result);
        $this->display();
    }

这里直接给出方法,modify的作用就是用户点击了修改按钮后,直接跳转到修改页面。并在修改页面显示出用户需要修改的内容。

  • index视图
<!DOCTYPE html>
<html>
<head>
    <title>用户表</title>
</head>
<style type="text/css">
    table{
        border: 1px solid #DCDCDC;
    }
    table td{
        border: 1px solid #DCDCDC;
    }
</style>
<body>
    <table style="margin: 0 auto;">
        <thead>
            <tr>
                <td>id</td>
                <td>用户名</td>
                <td>用户密码</td>
                <td>操作</td>
            </tr>
        </thead>
        <tbody>
            <volist name="data" id="row">
                <tr>
                    <td>{$row.id}</td>
                    <td>{$row.username}</td>
                    <td>{$row.userpwd}</td>
                    <td>
                        <a href="/CloudCrypt/index.php/Home/User/del?id={$row.id}">删除</a>|
                        <a href="/CloudCrypt/index.php/Home/User/modify?id={$row.id}">修改</a>
                    </td>
                </tr>
            </volist>
        </tbody>
    </table>
    <div style="margin: 0 auto;">
            <button type="button" onclick="jump();">添加用户</button>   
    </div>
</body>
</html>
  • modify视图
<!DOCTYPE html>
<html>
<head>
    <title>修改</title>
</head>
<body>
    <form action="/CloudCrypt/index.php/Home/User/update" method="post">
        用户名:<input type="text" value="{$data.username}" name="username"><br>&nbsp;码:<input type="text" name="userpwd" value="{$data.userpwd}"><br>
        <input type="hidden" name="id" value="{$data.id}">
        <button type="submit">提交</button>
    </form>
</body>
</html>

四、添加操作


在添加操作中,直接给出所有的代码

  • UserContoller.class.php
<?php
namespace HomeController;
use ThinkController;
class UserController extends Controller {
    public function index(){
        $user = M('User');
        $result = $user->select();
        $this->assign('data', $result);
        $this->display();
    }

    public function del()
    {
        $user = M('User');
        $id = $_GET['id'];
        $count = $user->delete($id);
        if($count > 0){
            $this->success("数据删除成功");
        }else{
            $this->error("数据删除失败!");
        }
    }

    public function modify(){
        $user = M('User');
        $id = $_GET['id'];
        $result = $user->find($id);
        $this->assign('data', $result);
        $this->display();
    }

    public function update(){
        $data['id']=$_POST['id'];
        $data['username'] = $_POST['username'];
        $data['userpwd'] = $_POST['userpwd'];
        $user = M('User');
        $count=$user->save($data);
        if($count>0){
            $this->success('数据修改成功','index');
        }else{
            $this->error("数据修改失败!");
        }
    }

    public function add(){
        $this->display();
    }
   
    public function create(){
        $user = M('User');
        $user->username = $_POST['username'];
        $user->userpwd = $_POST['userpwd'];
        $idNum = $user->add();
        if($idNum > 0){
            $this->success("数据添加成功", 'index');
        }else{
            $this->error('数据添加失败', 'index');
        }
    }
}
  • 视图层
  • index.html
<!DOCTYPE html>
<html>
<head>
    <title>用户表</title>
</head>
<style type="text/css">
    table{
        border: 1px solid #DCDCDC;
    }
    table td{
        border: 1px solid #DCDCDC;
    }
</style>
<body>
    <table style="margin: 0 auto;">
        <thead>
            <tr>
                <td>id</td>
                <td>用户名</td>
                <td>用户密码</td>
                <td>操作</td>
            </tr>
        </thead>
        <tbody>
            <volist name="data" id="row">
                <tr>
                    <td>{$row.id}</td>
                    <td>{$row.username}</td>
                    <td>{$row.userpwd}</td>
                    <td>
                        <a href="/CloudCrypt/index.php/Home/User/del?id={$row.id}">删除</a>|
                        <a href="/CloudCrypt/index.php/Home/User/modify?id={$row.id}">修改</a>
                    </td>
                </tr>
            </volist>
        </tbody>
    </table>
    <div style="margin: 0 auto;">
            <button type="button" onclick="jump();">添加用户</button>   
    </div>
    <script type="text/javascript">
    function jump () {
        alert('dd');
        window.location = "/CloudCrypt/index.php/Home/User/add";
    }
    </script>
</body>
</html>
  • add.html
<!DOCTYPE html>
<html>
<head>
    <title>用户表</title>
</head>
<style type="text/css">
    table{
        border: 1px solid #DCDCDC;
    }
    table td{
        border: 1px solid #DCDCDC;
    }
</style>
<body>
    <table style="margin: 0 auto;">
        <thead>
            <tr>
                <td>id</td>
                <td>用户名</td>
                <td>用户密码</td>
                <td>操作</td>
            </tr>
        </thead>
        <tbody>
            <volist name="data" id="row">
                <tr>
                    <td>{$row.id}</td>
                    <td>{$row.username}</td>
                    <td>{$row.userpwd}</td>
                    <td>
                        <a href="/CloudCrypt/index.php/Home/User/del?id={$row.id}">删除</a>|
                        <a href="/CloudCrypt/index.php/Home/User/modify?id={$row.id}">修改</a>
                    </td>
                </tr>
            </volist>
        </tbody>
    </table>
    <div style="margin: 0 auto;">
            <button type="button" onclick="jump();">添加用户</button>   
    </div>
    <script type="text/javascript">
    function jump () {
        alert('dd');
        window.location = "/CloudCrypt/index.php/Home/User/add";
    }
    </script>
</body>
</html>
View Code

  

 

原文地址:https://www.cnblogs.com/xidongyu/p/5563247.html