一.準備工作
- 開發環境集成工具:phpstudy,thinkphp3.2.3
- 將thinkphp3.2.3放在WWW目錄下,在phpstudy域名管理中配置域名www.thinkphp323.com,
- 在hosts中添加127.0.0.1 www.thinkphp323.com,
- 在瀏覽器中訪問將會看到如下:
二.創建數據
控制器
模型
(1)創建數據庫thinkphp323和數據表think_form
create database thinkphp323;
use thinkphp323;
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)在/Application/Home/Conf/config.php中配置數據庫信息
<?php
return array(
//'配置項'=>'配置值'
'DB_TYPE'=> 'mysql', // 數據庫類型
'DB_HOST'=> 'localhost', // 數據庫服務器地址
'DB_NAME'=>'thinkphp323', // 數據庫名稱
'DB_USER'=>'root', // 數據庫用戶名
'DB_PWD'=>'root', // 數據庫密碼
'DB_PORT'=>'3306', // 數據庫端口
'DB_PREFIX'=>'think_', // 數據表前綴
'DB_CASE_LOWER'=>true,
);
(3) 在/Application/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'),
);
}
- 視圖
<form action="__URL__/insert" class="form" method="post">
<div class="form-group">
<label for="title">標題</label>
<input type="password" class="form-control" id="title" placeholder="請輸入標題">
<div class="form-group">
<label for="">內容</label>
<textarea class="form-control" rows="6"></textarea>
<button type="submit" class="btn btn-primary tj">提交</button>
</form>