Heim >php教程 >php手册 >ThinkPHP 的CURD 基本操作,thinkphpcurd

ThinkPHP 的CURD 基本操作,thinkphpcurd

WBOY
WBOYOriginal
2016-06-13 09:03:32898Durchsuche

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn