-1

I have a one form with more than 30 fields. I know how to submit the data in the database using CodeIgniter. I just want to know that is there any other option to submit the data without writing the whole input postcode. I mean check below code. It looks very big.

$reg_dat = array(
    'surname' => $this->input->post('surname'),
    'name' => $this->input->post('firstname'),
    'age' => $this->input->post('age'),
    'school' =>$this->input->post('school'),
    'course' =>$this->input->post('course'),
    'email' => $this->input->post('email')

    'a' => $this->input->post('a'),
    'b' => $this->input->post('b'),
    'c' => $this->input->post('c'),
    'd' =>$this->input->post('d'),
    'e' =>$this->input->post('e'),
    'f' => $this->input->post('f')

    'g' => $this->input->post('g'),
    'h' => $this->input->post('h'),
    'i' => $this->input->post('i'),
    'j' =>$this->input->post('j'),
    'k' =>$this->input->post('k'),
    'l' => $this->input->post('l')

     and so on..
);

Is there any other option to make the above code in the small code?

always-a-learner
  • 3,671
  • 10
  • 41
  • 81
  • Now I am confused, I got two different an answer which is the best now @Himanshu or Keith? –  Oct 10 '17 at 11:25
  • Possible duplicate of [Is it possible to get all post variables in ExpressionEngine, like you could in CodeIgniter?](https://stackoverflow.com/questions/11128198/is-it-possible-to-get-all-post-variables-in-expressionengine-like-you-could-in) – always-a-learner Oct 10 '17 at 11:55
  • @ankitsuthar, If the question id duplicate than why you updated your answer here. –  Oct 10 '17 at 12:33

4 Answers4

1
$all_data = $_POST;

Above mentioned line will make an array with keys same as the column name as you mentioned in your question, so there is no need to write so many lines are you shown in your post.

OR

$all_data = $this->input->post(NULL, TRUE);  // returns all POST items with XSS filter. This is secure way.

And then to insert all that data in DB, we have syntax in Codeigniter is:

$this->db->insert('table_name',$all_data);  // Write this in your model.

So in table_name all the data will be inserted.

Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
  • sure. @Hybreeder. – Himanshu Upadhyay Oct 10 '17 at 11:08
  • Is it mandatory that form fields name and table columns name should be same? –  Oct 10 '17 at 11:12
  • Its not mandatory, but if they won't be same then you can not have approach that we suggested in answers. Actually the array you would pass to insert code of Codeigniter, should have the keys same as column names. So if your form elements have the same name as column, it will be helpful. – Himanshu Upadhyay Oct 10 '17 at 11:15
  • Can you help me in this https://stackoverflow.com/questions/46666787/validation-is-not-working-in-codeigniter/46666962#46666962 –  Oct 10 '17 at 13:01
  • @Hybreeder, I am talking about this one only. I meant to say, as my answer has solved your issue then can you accept this ? – Himanshu Upadhyay Oct 10 '17 at 13:06
  • Yes, I am getting popup from Stackoverflow that I will accept your answer after 11 mins –  Oct 10 '17 at 13:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156362/discussion-between-hybreeder-and-himanshu-upadhyay). –  Oct 10 '17 at 13:22
1

While using all $_POST elements can be quick, it's probably best to whitelist the ones you want to insert.

$whitelist = array('a', 'b', 'c', 'd' ...);

$reg_dat = array();

foreach($key => $value in $whitelist){
    $reg_dat[$value] = $this->input->post($value);
}

In terms of security, ensuring that you're using XSS filters and any other validation you may want to do on these elements prior to insertion. Also, make sure that your database driver is mysqli, pdo or some other, more secure one than mysql.

Based on your below comment about the $_POST keys not lining up with your database columns, you could do the following.

$whitelist = array();
$whitelist['a'] = 'database_column_for_a';
$whitelist['b'] = 'database_column_for_b';

$reg_dat = array();

foreach($postKey => $databaseColumn in $whitelist){
    $reg_dat[$databaseColumn] = $this->input->post($postKey);
}
kchason
  • 2,836
  • 19
  • 25
  • Can you help me in this link https://stackoverflow.com/questions/46666787/validation-is-not-working-in-codeigniter/46666962#46666962 –  Oct 10 '17 at 13:01
0

If you have POST fields names and MySQL columns names exactly same then just do this

$reg_dat = $_POST

And enter that array into DB

Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
Umair Ayub
  • 19,358
  • 14
  • 72
  • 146
0

Simply Do:

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

That's all. You will get $reg_dat array which contains all post input field and you can directly insert it in Codeigniter like.

Note: For Use, this directly makes sure your input name and your column name must be same.

$this->db->insert('table_name',$reg_dat);

For get,

$this->input->get();
always-a-learner
  • 3,671
  • 10
  • 41
  • 81