CodeIgniter Forums

Full Version: [newbie] $this->db->insert(); problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Ingonyama[/eluser]
Hi, Im trying to use the insert method but its looking for a "submit" column in the database (obviously from the submit button).

Error message is:
"Unknown column 'submit' in 'field list'"
INSERT INTO `categories` (`name`, `description`, `submit`) VALUES ('test', 'test123', '')

Controller:
Code:
function add_category()
    {
        $data['heading'] = 'Add category';
        
        if ($_POST)
        {
            echo 'Post submitted<br />';
            print_r($_POST);
            $this->db->insert('categories', $_POST);
        }
        
        $this->load->view('admin/categories/add_category', $data);
    }

View:
Code:
<h1>&lt;?php echo $heading; ?&gt;</h1>
&lt;?php echo form_open('admin/add_category'); ?&gt;
<dl>
    <dt><label for="name" id="name_label"></label></dt>
    <dd>&lt;?php echo form_input('name'); ?&gt;</dd>
    <dt><label for="description" id="description_label"></label></dt>
    <dd>&lt;?php echo form_input('description'); ?&gt;</dd>
    <dd>&lt;?php echo form_button('submit', 'Submit'); ?&gt;</dd>
</dl>
&lt;?php echo form_close(); ?&gt;

Thanks for any help.

El Forum

[eluser]xwero[/eluser]
It's very tempting to add POST to the fields parameter but you have to be aware there could be other fields in the form that are not for the database.
So it's best to hardcode the values you need for the fields or you could use the $this->db->list_fields() method to filter the POST global.

El Forum

[eluser]Ingonyama[/eluser]
Thanks that makes sense, I just hardcoded it in.