添加数据ThinkPHP笔记
(1)创建数据表:
CREATE TABLE IF NOT EXISTS `think_form` (
`id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
...
(1)创建数据表:
CREATE TABLE IF NOT EXISTS `think_form` (
`id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`content` varchar(255) NOT NULL,
`create_time` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
(2)创建模版:/App/Home/View/Form/add.html
<FORM method="post" action="__URL__/insert">
标题:<INPUT type="text" name="title"><br/>
内容:<TEXTAREA name="content" rows="5" cols="45"></TEXTAREA><br/>
<INPUT type="submit" value="提交">
</FORM>
(3)创建控制器:/App/Home/Controller/FormController.class.php
<?php
namespace Home\Controller;
use Think\Controller;
class FormController extends Controller{
public function insert(){
$Form = D('Form');
if($Form->create()) {
$result = $Form->add();
if($result) {
$this->success('操作成功!');
}else{
$this->error('写入错误!');
}
}else{
$this->error($Form->getError());
}
}
}
(4)创建模型:/App/Home/Model/FormModel.class.php
<?php
namespace Home\Model;
use Think\Model;
class FormModel extends Model {
// 定义自动验证
protected $_validate = array(
array('title','require','标题必须'),
);
// 定义自动完成
protected $_auto = array(
array('create_time','time',1,'function'),
);
}
(5)访问:http://127.0.0.1/index.php/Home/Form/add
我要评论