首页 >后端开发 >php教程 >ThinkPHP 的CURD 基本操作,thinkphpcurd_PHP教程

ThinkPHP 的CURD 基本操作,thinkphpcurd_PHP教程

WBOY
WBOY原创
2016-07-13 09:52:45877浏览

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(),在控制层中,你要得到前台的传值。

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

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

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

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

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

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1007359.htmlTechArticleThinkPHP 的CURD 基本操作,thinkphpcurd 说起CURD,懂点SQL的人都知道,就是增删改查,做业务系统的时候,往往离不开这CURD,最近也是刚刚接触...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn