ci3前台留言板

模型(增加)

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class About_Model extends CI_Model {

    protected $table_name = "blog_message";

    //调用类库

    public function __construct()
    {
        parent::__construct();
    }



    //过滤字段
    protected function filter_field($data)
    {
        $fields = $this->db->list_fields($this->table_name);//获取数据库字段


        foreach($data as $key=>$val){
            if(!in_array($key,$fields)){
                unset($data[$key]);
            }
        }
        return $data;
    }

    //添加
    public function addMessage($data){
        $data = $this->filter_field($data);
        $res = $this->db->insert($this->table_name,$data);

        if($res){
            return $this->db->insert_id();
        }else{
            return false;
        }

    }




}

控制器

//添加
public function add()
{

    if($_SERVER['REQUEST_METHOD'] == "POST"){

        // 收集表单
        $data = array(
            'name' => $this->input->post('name'),
            'tel' => $this->input->post('tel'),
            'qq' => $this->input->post('qq'),
            'content' => $this->input->post('content'),
            'time'=>time()
        );



        // 实例化模型,完成入库
        $result = $this->about->addMessage($data);

        if($result){

            redirect('about/index');

        }
    }

    $base_url = $this->config->item('base_url');

    $this->load->view('about/index',array('base_url'=>$base_url));
}

视图

<form action="/index.php/about/add" method="post" class="message">

    姓名:<input type="text" name="name"/><br/><br/>

    手机: <input type="text" name="tel"/><br/><br/>

    QQ:  <input type="text" name="qq"/><br/><br/>

    留言:<textarea name="content" cols="22"></textarea><br/><br/>

    <input type="submit" value="Send" />

</form>

猜你喜欢

转载自blog.csdn.net/qq_40270754/article/details/84552127
今日推荐