TP3.0+ACCESS数据库操作【其版本没有测】

浏览:1136 发布日期:2017/01/19 分类:技术分享 关键字: ThinkPHP ACCESS TP3.0
客户的无理要求,必须用TP+ACCESS数据库做一个企业项目的网站;在网上,我也搜索了很多资料,都没有一篇完整并可以使用的教程;
废话不多说:我个人用了半天的时间做了一个简单的增删改查如下图:
列表页:

添加页:

修改页:


实现以上的步骤:
第一:先Conf文件夹下的config.php配置一些信息如下图:


在根目录添加Data目录创建一个access数据test.mdb
下一步在test.mdb数据中创建一个tp_user表
表结构如下:


第二步:在\Lib\Action\IndexAction.class.php文件中实现逻辑:代码如下
<?php
// 本类由系统自动生成,仅供测试用途class IndexAction extends Action {
    //列表
    public function index(){
        $model = M();
        $this->data = $model->query("select * from tp_user");
        $this->display();
    }

    //添加视图
    public function add(){
        $this->display();
    }

    //添加动作处理
    public function addHandle(){
        if (!$this->isPost()) {
            halt('页面不存在');
        }
        $Model = M();
        $username = $this->_post('username');
        $password = $this->_post('password','md5');
        $logintime = date('Y-m-d H:i:s',time());
        $id = $Model->execute("INSERT INTO tp_user(username,password,logintime) VALUES('{$username}','{$password}','{$logintime}')");

        if($id){
            $this->success('添加成功',U('index'));
        }else{
            $this->error('添加失败');
        }
    }

    //编辑视图
    public function edit($id){
        $id = $this->_get('id');
        $model = M();
        $data = $model->query("select * from tp_user where id=$id");
        $this->assign('data',$data);
        $this->display();
    }

    //编辑动作
    public function editHandle(){
        if(!$this->isPost()){
            halt('页面不存在');
        }
        $id = $this->_post('id');
        $username = $this->_post('username');
        $password = $this->_post('password','md5');
        $model = M();
        $id = $model->execute("update tp_user set username='{$username}',password='{$password}' where id={$id}");
        if($id){
            $this->success('添加成功',U('index'));
            exit;
        }else{
            $this->error('添加失败');
        }
    }

    //删除处理
    public function del(){
        $id = $this->_get('id');
        $model = M();
        $row = $model->execute("delete from tp_user where id={$id}");
        if($row){
            $this->success('删除成功',U('index'));
            exit;
        }else{
            $this->error('删除失败');
        }
    }
}
注:如果是通过ThinkPHP+ACCESS进行开发的话,TP里面CURD的方法都无法使用,例如:
$model = M("User");
$model->select();
会提示:DESCRIBE 这个错误;
所以操作表的都是通过原生的SQL语句进行操作。

query():查询
execute():插入,更新,删除
更多操作请看上述第二步。

最后上一些帮客户做的后台页面::





最佳答案
评论( 相关
后面还有条评论,点击查看>>