I have one table. I want to select a row through a unique Id and want to change some fields of that row. Although I am able to update the table, but whenever I update any particular column of a row, rest of the existing values of that row becomes null. Please help me in this. You can find my 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 for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /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 from leads where PhNo=? ORDER BY 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 the 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, I get those fields updated in that particular row, but the remaining fields are loosing their values, they are becoming null. What can be done to stop this. I want that user can edit the desired fields and hit submit. The query should only update those fields which are edited by the user, and should keep the values of rest of the fields as they are(not null).
Aucun commentaire:
Enregistrer un commentaire