如何在不更改codeigniter上该行的其他未编辑列字段的情况下更新表的多个列

时间:2016-06-11 11:40:36

标签: codeigniter

我有一张桌子。我想通过唯一ID选择一行,并希望更改该行的某些字段。虽然我能够更新表,但每当我更新一行的任何特定列时,该行的其余现有值都将变为空。请帮帮我。您可以在下面找到我的代码:

我的控制器:

<?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 */

我的模特:

<?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(); 


    }

}   

?>

查看代码:

<!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>

如果执行此操作,我会在该特定行中更新这些字段,但其余字段将丢失其值,它们将变为空。可以做些什么来阻止这一点。我希望该用户可以编辑所需的字段并点击提交。查询应该只更新用户编辑的那些字段,并且应该保留其余字段的值(非空)。

2 个答案:

答案 0 :(得分:0)

您可以使用

    if(!empty($_POST["orderid"])) {
        $updt['Order_Id'] = $_POST["orderid"];
    }

或:         if(isset($ _ POST [&#34; orderid&#34;])&amp;&amp; strlen($ _ POST [&#34; orderid&#34;])){             $ updt [&#39; Order_Id&#39;] = $ _POST [&#34; orderid&#34;];         }

和CI版本:

或:         if(!is_null($ this-&gt; input-&gt; post(&#39; Order_Id&#39;)){             $ updt [&#39; Order_Id&#39;] = $ this-&gt; input-&gt; post(&#39; Order_Id&#39;);         }

而不是:

    if(isset($_POST["orderid"])) {
        $updt['Order_Id'] = $_POST["orderid"];
    }

请检查输入库,有XSS过滤选项 - 有助于提高网站的安全性。

答案 1 :(得分:0)

尝试短代码版本......

public void CrawlItems(ItemQueue itemQueue)
{
    Parallel.ForEach(
        itemQueue,
        new ParallelOptions {MaxDegreeOfParallelism = 4},
        item =>
        {
            worker.Url = item;
            /* Some work */
         });
}

请记住将数据格式化为数组($ columnName =&gt; $ value)并附带相应的更新信息