How to update multiple column of a table without changing other unedited column fields of that row on codeigniter -
i have 1 table. want select row through unique id , want change fields of row. although able update table, whenever update particular column of row, rest of existing values of row becomes null. please me in this. can find code below:
my controler:
<?php if ( ! defined('basepath')) exit('no direct script access allowed'); class edit extends ci_controller { function __construct(){ parent::__construct(); $this->load->helper(array('form', 'url')); $this->load->model('welcome_model'); } /** * index page controller. * * maps following url * http://example.com/index.php/welcome * - or - * http://example.com/index.php/welcome/index * - or - * since controller set default controller in * config/routes.php, it's displayed @ http://example.com/ * * other public methods not prefixed underscore * map /index.php/welcome/<method_name> * @see http://codeigniter.com/user_guide/general/urls.html */ public function index() { $this->load->view('edit'); } /**function update_lead_id(){ if($_post) { $id = $this->input->post('lead_id'); unset($_post['lead_id']); $result = $this->welcome_model->update_lead_id($_post, $id); } } */ function update_lead_id() { if($_post) { $updt = array(); if(isset($_post["phno"])) { $updt['phno'] = $_post["phno"]; } if(isset($_post["r"] )) { $updt['r'] = $_post["r"]; } if(isset($_post["manufacturer"])) { $updt['manufacturer'] = $_post["manufacturer"]; } if(isset($_post["model"])) { $updt['model'] = $_post["model"]; } if(isset($_post["modelyear"])) { $updt['modelyear'] = $_post["modelyear"]; } if(isset($_post["engine"])) { $updt['engine'] = $_post["engine"]; } if(isset($_post["bodytype"])) { $updt['bodytype'] = $_post["bodytype"]; } if(isset($_post["city"])) { $updt['city'] = $_post["city"]; } if(isset($_post["part"])) { $updt['part'] = $_post["part"]; } if(isset($_post["rp"])){ $updt['rp'] = $_post["rp"]; } if(isset($_post["price"])) { $updt['price'] = $_post["price"]; } if(isset($_post["otherinfo"])) { $updt['otherinfo'] = $_post["otherinfo"]; } if(isset($_post["orderid"])) { $updt['order_id'] = $_post["orderid"]; } $id = $this->input->post('lead_id'); unset($_post['lead_id']); $result = $this->welcome_model->update_lead_id($updt, $id); echo $result; /**$id = $this->welcome_model->update_lead_id($update); echo $id;*/ } } } /* end of file welcome.php */ /* location: ./application/controllers/welcome.php */
my model:
<?php if ( ! defined('basepath')) exit('no direct script access allowed'); class welcome_model extends ci_model{ /** * @desc load both db */ function __construct(){ parent::__construct(); } function set_info($data){ print_r ($data); $this->db->insert("leads", $data); $id = $this->db->insert_id(); return $id; } function phno($phno){ $sql = "select lead_id, r, manufacturer, model, modelyear, city, time leads phno=? order time desc"; $query = $this->db->query($sql, array($phno)); return $query->result_array(); } function update_lead_id($updt, $id){ print_r($updt); $this->db->where('lead_id', $id); $this->db->update('leads', $updt); return $this->db->affected_rows(); } } ?>
view code:
<!doctype html> <html> <head> <title>edit sheet</title> <header class="header"> </header> </head> <body> <div class="edit_con"> <p>edit table<p> <form action="edit/update_lead_id" method="post"> <input type="number" claa="id" name="lead_id" placeholder="lead_id" required><br /> <input type="number" class="phno" name="phno" placeholder="phone number"><br /> <input type="text" class="r" name="r" placeholder="r" ><br /> <input type="text" class="manufacturer" name="manufacturer" placeholder="manufacturer" ><br /> <input type="text" class="model" name="model" placeholder="model" ><br /> <input type="text" class="modelyear" name="modelyear" placeholder="model year" ><br /> <input type="text" class="engine" name="engine" placeholder="engine" ><br /> <input type="text" class="bodytype" name="bodytype" placeholder="body type" ><br /> <input type="text" class="city" name="city" placeholder="city" ><br /> <input type="text" class="part" name="part" placeholder="part" ><br /> <input type="text" class="rp" name="rp" placeholder="rp" ><br /> <input type="text" class="price" name="price" placeholder="price" ><br /> <input type="text" class="otherinfo" name="otherinfo" placeholder="other info" ><br /> <input type="text" class="orderid" name="orderid" placeholder="order_id" ><br /> <input type="submit" class="buttun" value="edit" name="edit" /><br /><br /> </form> </div> </body>
if execute this, fields updated in particular row, remaining fields loosing values, becoming null. can done stop this. want user can edit desired fields , hit submit. query should update fields edited user, , should keep values of rest of fields are(not null).
you can use
if(!empty($_post["orderid"])) { $updt['order_id'] = $_post["orderid"]; }
or: if(isset($_post["orderid"]) && strlen($_post["orderid"])) { $updt['order_id'] = $_post["orderid"]; }
and ci version:
or: if(!is_null($this->input->post('order_id')) { $updt['order_id'] = $this->input->post('order_id'); }
instead of:
if(isset($_post["orderid"])) { $updt['order_id'] = $_post["orderid"]; }
please check input library, there xss filtering option - improving security of site.
Comments
Post a Comment