Welcome Guest, Not a member yet? Register   Sign In
Newbie trying to get form data into database
#1

[eluser]douglasbrownca01[/eluser]
I've built a form that successfully processes form data, builds an email and sends with validation and builds a thank you page on successful submit.

I'm trying to insert the form data into my database table and I can't find a clear tutorial that shows me the relationship between the controler and the model on how the form data is passed and then inserted. I've searched forums, code ignitor manual, can anyone point me to a good reference to show me how to execute this process?
#2

[eluser]xerobytez[/eluser]
Here is an example model. It is the models job to handle data of any sort, whether its from a database, file or web based API. Anything that has to do with handling data.

Code:
class Employee_Model extends CI_Model {  
public function insert_employee($first_name, $last_name, $email) {
  $employee = array(
   'first_name' => $first_name,
   'last_name' => $last_name,
   'email' => $email
  );
  
  return $this->db->insert('employee_table', $employee);
}
}

So you would load the model into your controller by using
Code:
$this->load->model('employee_model');

Then after validation and pulling the data from the form you would call

Code:
$this->employee_model->insert_employee($first_name, $last_name, $email);

Hope this gives you an idea of how things work.

#3

[eluser]douglasbrownca01[/eluser]
Thanks for the clarification. That all makes sense -- to make sure I understand, after the validation -- to pull the data into the variables I'm passing back to the model, would go something like this:

Code:
if ($this->form_validation->run() == FALSE)
  {
                  // rebuild form and bring back missing fields
  }
  else
  {


                 $first_name ->$this->input->post('first_name');
                 $last_name ->$this->input->post('last_name');
                 $email ->$this->input->post('last_name');
  

$this->employee_model->insert_employee($first_name, $last_name, $email);

  // build email and send
    
    }

Two questions -- are there any helpers or libraries I need to be loading and in my case, there are a lot of fields to insert -- 14 or so -- can that be done in a bit more efficient way than writing out every item in the form set either on the controller side or when I build the mysql insert in the model?
#4

[eluser]xerobytez[/eluser]
You got the idea but

Code:
$first_name ->$this->input->post('first_name');

should be

Code:
$first_name = $this->input->post('first_name');

Note the equal right after the variable name. In the particular example I posted you shouldn't need any additional libraries or helpers. You can pull all form fields into an array with the method below from the input class.

Code:
$form_data = $this->input->post();

//You can then do

$this->employee_model->insert_employee(
    $form_data['first_name'],
    $form_data['last_name'],
    $form_data['email']
);
#5

[eluser]douglasbrownca01[/eluser]
That's much clearer than the tutorials..everyone was all over the place and pretty convoluted. Missing the equal sign was a typo -- it's been a long day. I'll work this out first thing in the morning and see how it goes. Thanks!!!!
#6

[eluser]douglasbrownca01[/eluser]
That cleared it up and I've got the data submitting to the database. Thank you for your time and giving me a clear path to getting this straightened out.
#7

[eluser]CroNiX[/eluser]
[quote author="xerobytez" date="1360116196"]You got the idea but

Code:
$first_name ->$this->input->post('first_name');

should be

Code:
$first_name = $this->input->post('first_name');

Note the equal right after the variable name. In the particular example I posted you shouldn't need any additional libraries or helpers. You can pull all form fields into an array with the method below from the input class.

Code:
$form_data = $this->input->post();

//You can then do

$this->employee_model->insert_employee(
    $form_data['first_name'],
    $form_data['last_name'],
    $form_data['email']
);
[/quote]
That is very insecure to just insert the entire post array with no validation or checking of allowed fields. It's not hard to manipulate forms within the browser and add your own fields to submit...
#8

[eluser]douglasbrownca01[/eluser]
Thanks, I appreciate that follow up -- the insert doesn't happen until validation occurs, you just don't see it in the code example.
#9

[eluser]xerobytez[/eluser]
[quote author="CroNiX" date="1360261274"]
That is very insecure to just insert the entire post array with no validation or checking of allowed fields. It's not hard to manipulate forms within the browser and add your own fields to submit...[/quote]

Yes that would be a bad idea, however thats not what my code was showing. And as OP stated in his original post he is already performing validation on the form.
#10

[eluser]douglasbrownca01[/eluser]
Ah..I see what your saying. Thank you.




Theme © iAndrew 2016 - Forum software by © MyBB