Help Submit Button in codeigniter forms.

Hey everyone, I'm having an issue I hope you can help me with.. I am just starting with codeigniter and have been diligently following the blog screencast, step by step, however when submitting the comment form y run into the following problem:

It seems by submitting the form the submit button is also being passed on as a field to the database causing the following error message to display:

A Database Error Occurred

Error Number: 1054

Unknown column 'submit' in 'field list'

INSERT INTO `comments` (`entry_id`, `body`, `author`, `submit`) VALUES ('2', 'dfgdg', 'fdsf', '')

I finally got it to work by creating a field called submit on the DB, but obviously this is not normal. Am I making a NOOB mistake, I have gone through all the code and have been able to figure it out.

Anybody have any insight, btw I apologize in advance if this is a dumb question, but your help is throughly appreciated.

Below my code.


<?=form_hidden('entry_id', $this->uri->segment(3)); ?>
<p>&lt;textarea name="body" rows="10"&gt;&lt;/textarea></p>  
<p>&lt;input type="text" name="author" /&gt;&lt;/p>
&lt;?php echo form_submit('submit')?&gt;
function comment_insert()
       $this->db->insert('comments', $_POST);

A Database Error Occurred

Error Number: 1054

Unknown column 'submit' in 'field list'

INSERT INTO `comments` (`entry_id`, `body`, `author`, `submit`) VALUES ('2', 'dfgdg', 'fdsf', '')

I think it's because of your code here:

&lt;?php echo form_submit('submit')?&gt;

I believe it should be:

&lt;?php echo form_submit('submit', 'Submit')?&gt;

You left out the 2nd parameter.


Forget that. If the form submitted the 2nd parameter probably doesn't matter. I looked at your insert code again and realized that you are passing the $_POST array, which would include the name of the submit button. You should probably make a $data array from the $_POST values first and pass that along in your insert.

i would suggest that you use the associative array method... for sure you know this Smile but i guess it's one of the neat methods out there...

// validation here for security and data integrity reasons

$data = array(


The easiest way is to copy the $_POST array and leave out the submit key. Something like this.

foreach ($_POST as $key=>$value)
if ($key!="submit") $new_array[$key]=$value;
then insert the new_array into the db.

Although this might be too late
in case someone else interested

you can do either:
1- remove the "name" attribute from the input tag //hence won't be included in the $_POST array.
2- or use unset($_POST['Submit']) //this will remove the submit entry form the $_POST array.

then use your insert normally.
however it is not recommended to insert the data using $_POST directly without filtering for security.


$data = $this->input->post();

another alternative would be to not use the ci form libarary for the submit button, and go this route:

<button type="submit">Submit</button>

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.