質問

I'm trying to build a small blog with CodeIgniter, and for some reason I put form database entries 0 instead of strings. I tried to put it through the controller manually and it works. What's the problem in a form?

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class site_model extends CI_Model {

    var $title   = '';
    var $content = '';
    var $date    = '';

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function get_records()
    {
        $query = $this->db->get('posts');

        return $query->result();
    }

    function add_records($data) {

        $this->db->insert('posts', $data);

        return;
    }

    function updata_records($data) {

        $this->db->where('postID', $id);
        $this->db->updata('posts', $data);
    }

    function delede_roe() {

        $this->db->where('postID', $this->uri->segment(3));
        $this->db->delete('posts');
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

the form page:

    <div class="grid_11">
            <h2>About</h2>
        <?php  echo form_open('site/creata'); ?>

    <label for="title"> title </label>

    <input type="text" name="title" id="title">

    <label for="content"> content </label>

    <input type="text" name="content" id="content">

    <br>

    <input type="submit" value="send">

    <?php  echo form_close(); ?>
        </div>

the controler:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class site extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -  
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $this->load->view('home');
    }

    function add_post() {

        $this->load->view('add_post');
    }

    function creata() {

        $data = array (
            'title' => $this->input->post('title'),
            'post' => $this->input->post('content')
        );

        $this->load->model('site_model');
        $this->site_model->add_records($data);
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
役に立ちましたか?

解決

Your controller function should look something like this:

function creata() {
    if( $this->input->post(null) ){ //enter if the form is submitted
        $data = $this->input->post(null, true); // get all the data of the post
        print_r( $data );die; // see the post data 
        $this->load->model('site_model');
        $this->site_model->add_records($data);
        $this->session->set_flashdata('msg', 'A msg to show to the user about the insertion');
        redirect('site/creata', 'refresh');
    }else{  //load the form
        $this->load->view('home');
    }
}

You form looks okay to me tho. You model may contain one extra line to view the query.

function add_records($data) {
    $this->db->insert('posts', $data);
    $this->db->last_query(); die; //check the query it's executing.
    return;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top