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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.