简体   繁体   English

发生数据库错误,列“ title”不能为空

[英]a database error occurred column 'title' cannot be null

here is my site_model code get all data from database and insert data into database using Html form please help i don't know where i do mistake 这是我的site_model代码,从数据库中获取所有数据,并使用HTML表单将数据插入数据库中,请帮助我不知道我在哪里出错

class Site_model extends CI_Model
{
    public function __construct()
    {
    }

    // get all data from database
    public function get_records()
    {
        $query = $this->db->get('user_data');
        return $query->result();
    }    

    // add records into database by form
    public function add_record($formData)
    {
        $this->db->insert('user_data', $formData);
    }
}

than i load model into controller and get values in an array. 比我加载模型到控制器,并在数组中获取值。 and after create an array to send form values into database 在创建数组以将表单值发送到数据库后

class Site extends CI_Controller
{
    var $data = array();

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Site_model');
    }

    public function index()
    {
        //get data into array from model
        $data = $this->data;
        $data['user_info'] = $this->Site_model->get_records();

        // get data from html form and send to database
        $formData = array(
            'title' => $this->input->post('post_title'),
            'description' => $this->input->post('post_descrip')
            );

        // send record to model
        $this->Site_model->add_record($formData);
        $this->load->view('crud_viw', $data);
    }
}

my view code look like this 我的查看代码如下所示

<!DOCTYPE html>
<html>
<head>
    <title>Crud</title>
</head>
<body>

    // Html form to send values to database
    <form method="post">
        <input type="text" placeholder="Title" name="post_title"></input>
        <input type="text" placeholder="Description" name="post_descrip">       </input>
        <button type="submit">Submit</button>
    </from>

</body>
</html>

Change your index method as below. 如下更改索引方法。 You only have to enter data when post event occur 您只需要在发生发布事件时输入数据

public function index()
    {
        //get data into array from model
        $data = $this->data;
        $data['user_info'] = $this->Site_model->get_records();
        if($this->input->post()){  //<--- add this condition

        // get data from html form and send to database
        $formData = array(
            'title' => $this->input->post('post_title'),
            'description' => $this->input->post('post_descrip')
            );

        // send record to model
        $this->Site_model->add_record($formData);
       }
        $this->load->view('crud_viw', $data);
    }

在数据库中,列“ title”不应为null。因此在数据库中将“ title”标记为非null。希望对您有所帮助。

The simple way to prevent getting the a database error occurred column 'title' cannot be null is use ternary operator (?:) .If you have not posted value it will inserts empty string. 防止a database error occurred column 'title' cannot be null的简单方法是使用三元运算符(?:)来使a database error occurred column 'title' cannot be null 。如果尚未发布值,它将插入空字符串。

 $formData = array(
            'title' => isset($this->input->post('post_title'))?$this->input->post('post_title'):" ",
            'description' => isset($this->input->post('post_descrip'))?$this->input->post('post_descrip'):" "
            );

Hope it works. 希望它能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM