简体   繁体   中英

How to update record in codeigniter?

controller code

if($this->input->post('update'))
         {
             $n=$this->input->post('pps_pin');
             $e=$this->input->post('pps_address');
             $m=$this->input->post('stm_shipping_type');
             $a=$this->input->post('tsm_time_slot');
             $b=$this->input->post('pps_price');
             $id=$this->input->post('pps_slid');
         $this->Config_model->updaterecords($n,$e,$m,$id,$a,$b);
         redirect('Admin_ctrl/config/view_pincode_price');
         }
    }

model code

function updaterecords($n,$e,$m,$id,$a,$b)
    {   
        $id;

        $qr=$this->db->query("UPDATE pincode_price_setup,shipping_type_mst,time_slot_mst SET pps_pin='$n',pps_address='$e',stm_shipping_type='$m',tsm_time_slot='$a',pps_price='$b' WHERE pps_slid='$id'");
        return $qr;
    }

Delivery Method and Delivery Time Slot data are same

You should modify your code to use codeigniters prepared statements. If I correctly understood relations in your DB, your update records method should looks something like this:

function updaterecords($n, $e, $m, $id, $a, $b)
{
    $this->db->set('pps_pin', $n);
    $this->db->set('pps_address', $e);
    $this->db->set('stm_shipping_type', $m);
    $this->db->set('tsm_time_slot', $a);
    $this->db->set('pps_price', $b);
    $this->db->join('shipping_type_mst', 'pincode_price_setup.pps_shipping_type = shipping_type_mst.stm_sqlid');
    $this->db->join('time_slot_mst', 'pincode_price_setup.pps_time_slot = time_slot_mst.tsm_sqlid');
    $this->db->where('pps_slid', $id);
    $this->db->update('pincode_price_setup');
}

Friendly tip: give your variables some more meaningful names, it would be much easier to work with variable named $pin than $n.

Please try like this

//Controller Code
function update_record($id){ 
    $update_array = array(
       'pps_pin' => $this->input->post('pps_pin') ///add fields in array which updated  
    );
    $this->modal_name->update($id,update_array);//pass id & array which want to be update
}

//Modal code

function update($id,$array){
    return $this->db->where('id',$id)->update('TABLE_NAME',array);
}

So as I understand it, you want to update records in your database. I will not write it for you. I will show you an example.

This code may not be perfect but it works for my localhost project.

Create a function to update records in your controller.

Example:

public function update($id){

    $id = $this->input->post('id');

    //create $data array and add your data to array
    $data = array(
        'nazov_divizia'     =>  $this->input->post('nazov')
    );

    //send this $data array with $id to your update function in model
    $this->Divizia_model->updateData($id, $data);

    //you can create flash message if you want
    $this->session->set_flashdata('message', 'Údaje upravené');

    //redirect to your controller index function
    redirect(base_url().'divizia');
}

Create a function to update the record in your model.

Example:

public function updateData($id, $data)
{
    $this->db->where('id_divizia', $id);
    $this->db->update('divizia', $data);
}
 Try This code



function updaterecords($n, $e, $m, $id, $a, $b) {

             $this->db->set('pps_pin', $n);
             $this->db->set('pps_address', $e);
             $this->db->set('stm_shipping_type', $m);
             $this->db->set('tsm_time_slot', $a);
             $this->db->set('pps_price', $b);
             $this->db->where('pps_slid', $id);
             $this->db->update('pincode_price_setup');
             return ($this->db->affected_rows() != 1) ? false : true;
        }

Once codeigniter model is loaded then its default update function works

if($this->input->post('update'))
     {
        $data = [
                'pps_pin' => $this->input->post('pps_pin'),
                'pps_address' => $this->input->post('pps_address'),
                'stm_shipping_type' => $this->input->post('stm_shipping_type'),
                'tsm_time_slot' => $this->input->post('tsm_time_slot'),
                'pps_price' => $this->input->post('pps_price'),
                'pps_slid' => $this->input->post('pps_slid'),
            ];
     $this->Config_model->id = $id;
     $this->Config_model->update($data);

     redirect('Admin_ctrl/config/view_pincode_price');
     }
}

So I am sure we don't need to define a separate function in model for same purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM