CodeIgniter Forums
Update form data into database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Update form data into database (/thread-72589.html)



Update form data into database - Sovani Shreeniwas - 01-09-2019

I have below file in views
<html>
<head>
<title>My Form</title>
</head>
<body>
 
<?php echo validation_errors(); ?>
 
<?php echo form_open('form'); ?>
 
<h5>Cert No.</h5>
<input type="text" name="certno" value="" size="20" />
 
<h5>Site Name</h5>
<input type="text" name="location" value="" size="20" />
 
<h5>Work Date</h5>
<input type="date" name="workdate" value="" size="10" />
 
<h5>Date Of   Receipt</h5>
<input type="date" name="dateofreceipt" value="" size="10" />
 
<h5>Customer Name</h5>
<input type ="text" name="customername" value"" size="50"/>
</form>
 
</body>
</html>
 
 
And below in Controller:
<?php
 
class Form extends CI_Controller {
 
        public function index()
        {
                $this->load->helper(array('form', 'url'));
 
                $this->load->library('form_validation');
 
                if ($this->form_validation->run() == FALSE)
                {
                        $this->load->view('myform');
                }
                else
                {
                        $this->load->view('formsuccess');
                }
        }
}
 
With above I am able to create the form. Now I want to upload the data I entered in the form as per above to the mysql database (I have already created mysql databse & compatible table to suit above through PHPmyadmin).
Kindly suggest the required additional code in Controller, Model & View.


RE: Update form data into database - Wouter60 - 01-09-2019

Before $this->load->view('formsuccess'); insert this piece of code:
PHP Code:
$data = array(
 
  'certno' => $this->input->post('certno'),
 
  'location' => $this->input->post('location'),
 
  'workdate' => $this->input->post('workdate'),
 
  'dateofreceipt' => $this->input->post('dateofreceipt'),    
   
'customername' => $this->input->post('customername'), 
);
$this->db->insert('mytablename'$data); 

Alternatively you can create a model that stores the information, but that won't make your code much shorter.
If you need to use the same database actions repeatedly in different functions in your controller (or different controllers), then consider using a model.


RE: Update form data into database - Sovani Shreeniwas - 01-10-2019

(01-09-2019, 11:57 AM)Wouter60 Wrote: Before $this->load->view('formsuccess'); insert this piece of code:
PHP Code:
$data = array(
 
  'certno' => $this->input->post('certno'),
 
  'location' => $this->input->post('location'),
 
  'workdate' => $this->input->post('workdate'),
 
  'dateofreceipt' => $this->input->post('dateofreceipt'),    
   
'customername' => $this->input->post('customername'), 
);
$this->db->insert('mytablename'$data); 

Alternatively you can create a model that stores the information, but that won't make your code much shorter.
If you need to use the same database actions repeatedly in different functions in your controller (or different controllers), then consider using a model.

Thanks for the valuable inputs from you. Actually I have many fields & need repeatedly, request you to suggest what code is needed in controller & what code in Model & what in views.


RE: Update form data into database - qury - 01-10-2019

(01-10-2019, 05:09 AM)Sovani Shreeniwas Wrote: Thanks for the valuable inputs from you. Actually I have many fields & need repeatedly, request you to suggest what code is needed in controller & what code in Model & what in views.

Hi Sovani,
There is no substitute for reading the manual in this case:
https://www.codeigniter.com/user_guide/tutorial/news_section.html

The tutorial describes what is the best way to use controller and model