Welcome Guest, Not a member yet? Register   Sign In
Inserting form fields into a database
#1

[eluser]bcarter[/eluser]
Hi there

Yet another noob question I'm afraid....

Can someone tell me if there is a snippet of code I can use to insert multiple $_POST values into a database instead of declaring each one? I'll try and show you what I mean.

Currently I am using the below code
Code:
$insert = array(
         'q1'=>$this->input->post('q1'),
         'q2'=>$this->input->post('q2'),
         'q3'=>$this->input->post('q3'),
         'q4'=>$this->input->post('q4'),
         'q5'=>$this->input->post('q5'),
         'q6'=>$this->input->post('q6'),
         'q7'=>$this->input->post('q7'),
         'q8'=>$this->input->post('q8'),
         'q9'=>$this->input->post('q9'),
         'q10'=>$this->input->post('q10'),
         'q11'=>$this->input->post('q11')
);
            
  $this->db->insert('results', $insert);

This works fine however there are actually 56 fields. Is there some code that I can use that would put all the values from the form fields into the $insert array?

Thanks for your help!
#2

[eluser]Clifford James[/eluser]
try using this in your input fields:
Code:
<input name="question[]" />
<input name="question[]" />
or
Code:
<input name="question[0]" />
<input name="question[1]" />

Then with codeigniter $this->input->post('question') is the array you want.
#3

[eluser]Unknown[/eluser]
If your variables from the View, match the Database columns, you can insert the whole $_POST. Given, that your insert function in the model has ability to take Array.
#4

[eluser]strike312[/eluser]
I am doing the same thing and I have the form fields the same as the columns in the database.

I seem to be getting a problem, the 'submit' is being taken as a column and therefore causing issues.

This is the error:
Quote:Error Number: 1054

Unknown column 'submit' in 'field list'

INSERT INTO `lungs` (`Q1`, `Q2`, `Q3`, `submit`) VALUES ('blach', 'blache', 'blachey', '')

Filename: C:\wamp\www\CI\system\database\DB_driver.php

Line Number: 330


And this is the code in the model:

Code:
$insert= $this->db->insert($dest,$this->input->post());


And the code in the view:

Code:
echo form_input('Q1','Question 1');
echo form_input('Q2','Question 2');
echo form_input('Q3','Question 3');

echo form_submit('submit','submit');
#5

[eluser]strike312[/eluser]
Ah i managed to solve the error, it seems if i change the form_submit code line to this:

Code:
echo form_submit('','Submit');

Now it works, but is this the best thing to do I am wondering? Leaving the submit name as '', will that have a side effect somewhere else?

Thanks.
#6

[eluser]bcarter[/eluser]
Is there really a need to include submit in the database?




Theme © iAndrew 2016 - Forum software by © MyBB