PHP头条
热点:

ThinkPHP 的CURD 基本操作,thinkphpcurd


  说起CURD,懂点SQL的人都知道,就是增删改查,做业务系统的时候,往往离不开这CURD,最近也是刚刚接触ThinkPHP,ThinkPHP的灵活性是比原生PHP好用的多,下面我就简单的介绍一下我的学习心得。

      学习ThinkPHP对MySQL的操作,首先你要有MySQL,然后又PHP的运行环境。

      wamp可以帮你解决配置的麻烦,关于wamp资料很多,百度就可以了。

      下面就简单介绍一下ThinkPHP的增删改查的过程。

      一、创建数据库,命名为t_user。

           代码为:

     

 DROP TABLE IF EXISTS `t_user`;
      CREATE TABLE `t_user` (
      `userid` int(11) NOT NULL,
      `username` varchar(25) DEFAULT NULL,
      `usersex` varchar(6) DEFAULT NULL,
      `userage` int(6) DEFAULT NULL,
      PRIMARY KEY (`userid`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  创建一个项目,命名为thinkPHP_Text,导入thinkphp核心包。

  配置index.php文件。

     启动项目,自动生成目录。如下图:

     

  二、关于thinkphp的add()操作。

           创建index的action文件,命名为IndexController.class.php,写一个函数insertUser(),在控制层中,你要得到前台的传值。

/**
* 添加用户信息
* 编码时间:2015-05-28
*/
public function insertUser($id,$name,$sex,$age){
$this->db(1,"DB_CONFIG1")->db(1);
$condition = array(//定义要添加的数据,放在一个数组里,命名为$condition
'userid' => $id,
'username' => $name,
'usersex' => $sex,
'userage' => $age,
);
$addInfo = $this->db(1,"DB_CONFIG1")->add($condition);//执行sql语句,insert
if($addInfo){
header("Location: http://localhost/thinkPHP_Text/index.php"); 
}
echo $this->getLastSql();//调试用,输出sql语句
return $addInfo; 
}
/**

         在model层中,记住命名方式,在本次配置中,命名为UserModel.class.php,对应的:

 1  /**
 2    * 添加用户信息
 3    * 编码时间:2015-05-28
 4  */
 5     public function insertUser($id,$name,$sex,$age){
 6             $this->db(1,"DB_CONFIG1")->db(1);
 7             $condition = array(//定义要添加的数据,放在一个数组里,命名为$condition
 8                 'userid' => $id,
 9                 'username' => $name,
10                 'usersex' => $sex,
11                 'userage' => $age,
12             );
13             $addInfo = $this->db(1,"DB_CONFIG1")->add($condition);//执行sql语句,insert
14             if($addInfo){
15                     header("Location: http://localhost/thinkPHP_Text/index.php"); 
16                 }
17             echo $this->getLastSql();//调试用,输出sql语句
18             return $addInfo;                
19         }

 这就是添加操作的核心代码。

具体的代码请到下面的链接下载,详细见注释:

http://pan.baidu.com/s/1hq7wfnm

www.phpzy.comtrue/php/48993.htmlTechArticleThinkPHP 的CURD 基本操作,thinkphpcurd 说起CURD,懂点SQL的人都知道,就是增删改查,做业务系统的时候,往往离不开这CURD,最近也是刚刚接触ThinkPHP,ThinkPHP的灵活性是比原生PHP好用的多,...

相关文章

PHP之友评论

今天推荐