【问题标题】:CodeIgniter put 0 instead of string values to a databaseCodeIgniter 将 0 而不是字符串值放入数据库
【发布时间】:2014-05-09 09:49:52
【问题描述】:

我正在尝试使用 CodeIgniter 构建一个小型博客,出于某种原因,我将表单数据库条目设置为 0 而不是字符串。我试图手动将它通过控制器并且它可以工作。表格有什么问题?

<?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 */

表单页面:

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

控制者:

<?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 */

【问题讨论】:

    标签: database forms codeigniter


    【解决方案1】:

    您的控制器函数应如下所示:

    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');
        }
    }
    

    你的形式在我看来还不错。您的模型可能包含额外的一行来查看查询。

    function add_records($data) {
        $this->db->insert('posts', $data);
        $this->db->last_query(); die; //check the query it's executing.
        return;
    }
    

    【讨论】:

    • 感谢您的回复。重要的是要注意该脚本在 localhost 上运行良好,只有当我将它上传到我的服务器时才会出现问题。我检查了数据库和相同的设置。提出修改也解决不了问题
    • 您是否从url 中删除了index.php?它在服务器上工作正常吗?
    • 是的,我添加了一个 htaccess 文件。一切正常,除了这个 FORM
    • 是的,我从配置文件中的 base_url 中删除了域,它成功了。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2016-11-28
    • 1970-01-01
    相关资源
    最近更新 更多