lwcompany

功到自然成

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::

一、基础设置

 

一、使用原表think_form表

二、配置index.html
    define('THINK_PATH','./ThinkPHP/');

    define('APP_NAME','Myapp');
 
    define('APP_PATH','./Myapp/');
   
    require(THINK_PATH.'/ThinkPHP.php');

    $App = new App();
   
?>

三、配置config.class.php

<?php
if (!defined('THINK_PATH')) exit();
return array(
'DB_TYPE'=>'mysql', // 使用的数据库是mysql
'DB_HOST'=>'localhost',
'DB_NAME'=>'myapp',// 数据库名
'DB_USER'=>'root',
'DB_PWD'=>'',// 填写你连接数据库的密码
'DB_PORT'=>'3306',
'DB_PREFIX'=>'think_', // 数据表表名的前缀 请参看
'URL_MODEL'=>2,  //rewrite
'APP_DEBUG'=>true,
'SHOW_PAGE_TRACE'=>true,
'SHOW_ERROR_MSG'=> true,
'url_model'=>1,
);
?>

 

二、单表查询

       UserAction.class.php

<?php
class UserAction extends Action{
    public function index(){
        $user=M('user');
        $list=$user->field(array('id','username','password','createip','createtime'))->select();
        $this->assign('title','thinkp单表查询');
        $this->assign('alist',$list);
        $this->display();       
    }

}

?>

     MODEL\UserModel.class.php

<?php
class UserModel extends Model{
    //此处暂时不写一些封装好的高级功能和自动义功能
   
}

?>

     tpl\user\index.html

<html>
<head>
<title>{$title}</title>
</head>
<body>
       <volist name='alist' id='vo'>
      
                  用户名:<input type="text" name='username' value="{$vo.username}"/>
                   密码:<input type="text" name='password' value="{$vo.password}"/>
                   注册IP   <input type="text" name='createIP' value="{$vo.createip}"/>
                   注册时间<input type="text" name='createtime' value="{$vo.createtime}"/></br>
       <a href="__URL__/edit"> 操作</a>
       </volist>          
</body>
</html>

  运行结果:   
image

三 表单编辑

     一、indexaction.class.php 增加一个方法

public function edit(){
        $user=M('user');
        $id=(int)$_GET['id'];
        $list=$user->field(array('id','username','password','createip','createtime'))
                   ->where("id=$id")
                   ->select();
        $this->assign('title','单表编辑');
        $this->assign('alistedit',$list);
        $this->display();
    }

 

    二、tpl\user\edit.html

<html>
<head>
<title>{$title}</title>
</head>
<body>
<form action="__URL__/update" method="POST">
      
        
       <volist name="alistedit" id='vo'>
                  用户名:<input type="text" name='username' value="{$vo['username']}"/>
                   密码:<input type="text" name='password' value="{$vo['password']}"/>
                   创建IP:  <input type="text" name='createip' value="{$vo['createip']}"/>
                   创建时间:<input type="text" name='createtime' value="{$vo['createtime']}"/></br>
       <input type="submit" name='save' value='保存'/>
       <input type="hidden" value="{$vo['id']}" name='id'></input>
    
      </volist>
</form>          
</body>
</html>

   运行结果:

K8`PZBRFL{$53[GG}2LROAK[6]

四、表单更新

    一、在index.class.php增加一个方法

public function update(){   
                $user=M('user');
                if($user->create()){
                    $user->password=md5($user->password);
                    if($inserid=$user->save()){
                            $this->success('更新成功,受影响行数为'.$inserid);
                    }else{
                            $this->error('更新失败,此处可以手动记录日志,供管理员查询,但是不要直接打印现来SQL语句啊');
                    }
                }else{
                    $this->error($user->getError());
                }
        }
}

  五、表单删除

     一、  在index.html里 

      <volist name='alist' id='vo'>
      
                  用户名:<input type="text" name='username' value="{$vo.username}"/>
                   密码:<input type="text" name='password' value="{$vo.password}"/>
                   注册IP   <input type="text" name='createIP' value="{$vo.createip}"/>
                   注册时间<input type="text" name='createtime' value="{$vo.createtime}"/></br>
      <a href="__URL__/edit/id/{$vo['id']}"> 编辑</a>
       <a href="__URL__/del/id/{$vo['id']}"> 删除</a>
       </volist>

     二、在IndexAction.class.php类中增加一个del()方法

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

posted on 2012-09-11 14:01  平渡飞扬  阅读(1176)  评论(0编辑  收藏  举报