CodeIgniter Forums

Full Version: Help Submit Button in codeigniter forms.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Unknown[/eluser]
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.



View
Code:
<?=form_open('blog/comment_insert');?>

<?=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;
&lt;/form&gt;
Controller
Code:
function comment_insert()
    {
      
       $this->db->insert('comments', $_POST);
       redirect('blog/comments/'.$_POST['entry_id']);
    
    }

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', '')
[/b]

El Forum

[eluser]Dunrobin[/eluser]
I think it's because of your code here:

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

I believe it should be:

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

You left out the 2nd parameter.

UPDATE:

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.

El Forum

[eluser]therealmaloy[/eluser]
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...

Code:
// validation here for security and data integrity reasons

$data = array(
'entry_id'=>$this->input->post('entry_id'),
'body'=>$this->input->post('body'),
'author'=>$this->input->post('author'));

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

El Forum

[eluser]Clooner[/eluser]
The easiest way is to copy the $_POST array and leave out the submit key. Something like this.

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

El Forum

[eluser]himalking[/eluser]
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.

El Forum

[eluser]Rolly1971[/eluser]
use:

$data = $this->input->post();
unset($data['submit']);

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

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