微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

添加数据

(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 HomeController;

use ThinkController;

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 HomeModel;

use ThinkModel;

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐