?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
class MsgController extends Controller{
public function index(){
$msg = D('Msg');
$info = $msg->order('id DESC')->select();
$this->assign('info',$info);
$this->display();
}
public function sendMsg(){
$msg = new \Home\Model\MsgModel();
if (!empty($_POST)){
$data = $msg->create();
if($data){
$data['user_hobby'] = implode(',',$data['user_hobby']);
$z = $msg->add($data);
if ($z){
$this->redirect('Msg/sendMsg');
}
}else{
$this->assign('errorInfo',$msg->getError());
}
}
$this->display();
}
public function upd($id){
$msg = D('Msg');
if (!empty($_POST)){
$z = $msg->save($_POST);
if ($z){
$this->redirect('index',array(),2,'修改成功');
}else{
$this->redirect('upd',array('id'=>$id),2,'修改失敗');
}
}else{
$info = $msg->find($id);
$this->assign('info',$info);
$this->display();
}
}
public function addMsg(){
$msg = D('Msg');
if (!empty($_POST)){
$z = $msg->add($_POST);
if ($z){
$this->redirect('index',array(),2,'添加成功');
}else{
$this->redirect('addMsg',array(),2,'添加失敗');
}
}else{
$this->display();
}
}
public function del($id){
if(D('Msg')->delete($id)){
$this->success('成功',U('index'),2);
}else{
$this->error('失敗',U('index'),2);
}
}
}
?php
namespace Home\Model;
use Think\Model;
class MsgModel extends Model{
//是否批量驗證
protected $patchValidate = true;
protected $_validate = array(
array('title','require','標題不能為空!'), //默認情況下用正則進行驗證
array('user','require','留言人不能為空!'),
array('msg','require','內(nèi)容不能為空!'),
);
protected $_auto = array (
array('status','1'), // 新增的時候把status字段設置為1
array('id','NULL'),
array('admin_user','ms'),
array('replay','NULL'),
array('update_time','time',3,'function'), // 對update_time字段在更新的時候寫入當前時間戳
array('send_msg_time','time',3,'function'),
);
}
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- 資料庫: `msg`
--
-- --------------------------------------------------------
--
-- 表的結構 `ms_msg`
--
CREATE TABLE IF NOT EXISTS `ms_msg` (
`id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
`admin_user` varchar(100) NOT NULL COMMENT '管理員',
`update_time` int(10) NOT NULL COMMENT '更新時間',
`status` int(2) NOT NULL COMMENT '狀態(tài)',
`send_msg_time` int(10) NOT NULL COMMENT '留言時間',
`user` varchar(100) NOT NULL COMMENT '留言人',
`title` varchar(100) NOT NULL COMMENT '標題',
`msg` varchar(200) NOT NULL COMMENT '內(nèi)容',
`replay` varchar(200) NOT NULL COMMENT '回復',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='留言表' AUTO_INCREMENT=19 ;
--
-- 轉存資料表中的資料 `ms_msg`
--
INSERT INTO `ms_msg` (`id`, `admin_user`, `update_time`, `status`, `send_msg_time`, `user`, `title`, `msg`, `replay`) VALUES
(1, 'ms', 1479449110, 1, 1479449110, '1', '拉克絲的減肥', '對方科目了', 'NULL'),
(7, '', 321423432, 0, 0, 'kljflwk', 'kjsdfnlk', 'nlkdsjfn', 'kljnf'),
(3, 'ms', 1479451017, 1, 1479451017, '1', '輕松的發(fā)生我', '沃爾沃飛', 'NULL'),
(8, 'ms', 1479544687, 1, 1479544687, '', 'qwe', '', 'NULL'),
(9, 'ms', 1479544693, 1, 1479544693, 'qwe', 'qwe', 'qwe', 'NULL'),
(10, 'ms', 1479544970, 1, 1479544970, 'qwe', 'qwe', 'qwe', 'NULL'),
(11, 'ms', 1479544979, 1, 1479544979, '12', '12', '12', 'NULL'),
(12, 'ms', 1479545029, 1, 1479545029, '12', '12', '12', 'NULL'),
(13, 'ms', 1479546357, 1, 1479546357, '12', '12', '12', 'NULL'),
(14, 'ms', 1479547163, 1, 1479547163, '12', '12', '12', 'NULL'),
(16, 'ms', 1479547667, 1, 1479547667, '12', '12', '123', 'NULL'),
(17, 'ms', 2147483647, 1, 1479547682, '上來昆明3', '說的了付款', '藍山咖啡', '123213');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
更多關于thinkPHP相關內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結》、《ThinkPHP常用方法總結》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術總結》。