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

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.
Reply
#2

(This post was last modified: 01-09-2019, 01:23 PM by Wouter60.)

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.
Reply
#3

(01-09-2019, 10: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.
Reply
#4

(01-10-2019, 04: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/t...ction.html

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




Theme © iAndrew 2016 - Forum software by © MyBB