CodeIgniter Forums
codeigniter - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: General Help (
+--- Thread: codeigniter (/showthread.php?tid=61747)

codeigniter - bhumi - 05-13-2015

Hi I want to make copy(duplicate) a record in same page and updated in database too using codeigniter from model.table id haulers.I get company(id) as dropdown of company name ,but not the other fields.Can u help me out to get is my code.

function copy_hauler($hauler_id)

$query= $this->db->get();

'company' =>set_value('company'),
'hauler_name' =>set_value('hauler_name'),
'hauler_address_1' =>set_value('hauler_address_1'),
'hauler_city' =>set_value('hauler_city'),
'hauler_state' =>set_value('hauler_state'),
'hauler_zipcode' =>set_value('hauler_zipcode'),
'hauler_phone' =>set_value('hauler_phone'),
'hauler_fax' =>set_value('hauler_fax'),
'hauler_email' =>set_value('hauler_email'),
'hauler_website' =>set_value('hauler_website'),
'hauler_contact_name' =>set_value('hauler_contact_name'),
'hauler_contact_number' =>set_value('hauler_contact_number'),
'request_email' =>$this->input->post('request_email'),
'request_text' =>$this->input->post('request_text'),

if ($this->db->affected_rows() == '1')
return TRUE;

return FALSE;


RE: codeigniter - mwhitney - 05-13-2015

set_value() is meant to be used in a view, not a controller.

If you're submitting the data in a POST from a form, you should retrieve the data from $this->input->post(), as you've attempted with request_email and request_text.

If you just want to retrieve the data from the database based on an ID, as implied by your query at the beginning of the method, then you should fill your array from the query result, rather than attempting to fill it from $this->input->post().

Below are two (untested) examples, the first pulling the data from the POST, the second pulling it from the database.

PHP Code:
   // Insert the data submitted by the form
   public function copy_hauler($hauler_id)
       // Validate the form
       if ($this->form_validation->run() === false) {
           return false;

       // Get the POST data
       $following = array(
           'company'               => $this->input->post('company'),
           'hauler_name'           => $this->input->post('hauler_name'),
           'hauler_address_1'      => $this->input->post('hauler_address_1'),
           'hauler_city'           => $this->input->post('hauler_city'),
           'hauler_state'          => $this->input->post('hauler_state'),
           'hauler_zipcode'        => $this->input->post('hauler_zipcode'),
           'hauler_phone'          => $this->input->post('hauler_phone'),
           'hauler_fax'            => $this->input->post('hauler_fax'),
           'hauler_email'          => $this->input->post('hauler_email'),
           'hauler_website'        => $this->input->post('hauler_website'),
           'hauler_contact_name'   => $this->input->post('hauler_contact_name'),
           'hauler_contact_number' => $this->input->post('hauler_contact_number'),
           'request_email'         => $this->input->post('request_email'),
           'request_text'          => $this->input->post('request_text'),

       // Insert, return true on success, false on failure.
       return $this->db->affected_rows() == '1';

   // Insert the data retrieved from the database
   public function db_copy_hauler($hauler_id)
       $hauler_id = (int) $hauler_id;

       // Get the data for the provided ID

       $query $this->db->get();
       if ($query->num_rows() < 1) {
           // No data found, invalid ID?
           return false;

       // Get the first row of data, then populate $following for 
       // insert. You could just send $row, but you may want to do
       // something else with the data, first.
       $row $query->row_array();
       $following = array();
       foreach ($row as $key => $value) {
           $following[$key] = $value;

        // Insert, return true on success, false on failure.
       return $this->db->affected_rows() == '1';

   protected function getValidationRules()
       return array(
               'field' => 'company',
               'label' => 'Company',
               'rules' => 'max_length[255]'