![]() |
Newbie trying to get form data into database - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Newbie trying to get form data into database (/showthread.php?tid=56997) |
Newbie trying to get form data into database - El Forum - 02-05-2013 [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? Newbie trying to get form data into database - El Forum - 02-05-2013 [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 { 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. Newbie trying to get form data into database - El Forum - 02-05-2013 [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) 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? Newbie trying to get form data into database - El Forum - 02-05-2013 [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(); Newbie trying to get form data into database - El Forum - 02-05-2013 [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!!!! Newbie trying to get form data into database - El Forum - 02-07-2013 [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. Newbie trying to get form data into database - El Forum - 02-07-2013 [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(); 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... Newbie trying to get form data into database - El Forum - 02-07-2013 [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. Newbie trying to get form data into database - El Forum - 02-07-2013 [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. Newbie trying to get form data into database - El Forum - 02-07-2013 [eluser]douglasbrownca01[/eluser] Ah..I see what your saying. Thank you. |