【问题标题】:CodeIgniter update_batch without replacing the previous data next updated value will puts by comma separated as incrementCodeIgniter update_batch 不替换先前的数据下一个更新的值将用逗号分隔为增量
【发布时间】:2018-11-09 02:51:50
【问题描述】:

虽然我不确定。 update_batch 是否可以在不替换先前数据的情况下,下一个更新值将用逗号分隔为 SAME FILED 上的数组形式。我展示了 2 个演示图像以便更好地理解。我的代码已经在运行,仅用于批量更新,但不适用于 update_batch 增量值,无需替换以前的数据!

例如—— 第一次更新时,“t_franchise_price”栏的栏位会这样显示 ---

第二次执行下一次更新时,列字段“t_franchise_price”将如下所示 ---

我的控制者是

public function batch_update() {

    $sID = $this - > input - > post('test_id[]');
    $sAmt = $this - > input - > post('test_priceUpdt[]');
    $sOfficeId = $this - > input - > post('fran_office_id');
    date_default_timezone_set('Asia/Kolkata');
    $edited_test = array();

    if (!empty($sID)) {
        foreach($sID as $k => $v) {
            $edited_test = array(
                'test_id' => $sID[$k],
                'test_priceUpdt' => $sAmt[$k],
                'fran_office_id' => $sOfficeId,
                'last_edit_date' => date('Y-m-d H:i:s', time())
            );

            $edited_test = json_encode($edited_test);
            $postData[] = array('test_id' => $v, 't_franchise_price' => $edited_test);
        }
        $data['franchise_tst_pkg'] = (object) $postData;
    }

    
    #-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - #
    if ($this - > form_validation - > run() === true) {

        $this - > franchise_price_model - > batchTestupdate($postData);

        $this - > session - > set_flashdata('message', display('save_successfully'));
        redirect('branch/franchise_price/create');


}

**我的模特是**

public function batchTestupdate($data = [])
		{
			$this->db
				->update_batch($this->table_test, $data , 'test_id');
		}

查看

<tr>
  <td>
    <input type="text" name="test_name[]" class="form-control" placeholder="" value="<?php echo $subject->test_name; ?>" disabled>
    <input type="hidden" name="test_id[]" class="form-control" placeholder="" value="<?php echo $subject->test_id; ?>">
  </td>
  <td>
    <input type="text" name="test_price[]" class="form-control" placeholder="" value="<?php echo $subject->test_price; ?>" disabled>
  </td>
  <td>
    <input type="text" name="test_priceUpdt[]" class="form-control" placeholder="" value="<?php echo $subject->test_price; ?>" id="test_priceEdt">
  </td>
</tr>

【问题讨论】:

  • 你试过自己调试吗?找出$data 包含在函数batchTestupdate(...)var_dump($data); exit(); 中的内容。然后回溯,你应该能够弄清楚。我认为表单中的数组输入是导致 JSON 正在扩展而不是完全更新的问题的根源
  • 先生,在第一次更新时,我得到了所有行的值作为 id 明智的,正如预期的那样。但我的查询是,下次我更新同一个字段时,以前存储的数据将替换为新的条目数据。我不想删除或替换,想用 update_batch 保留以前和最近更新的数据。它显示在 2 个演示图像中。
  • @KarloKokkak 当然,先生。谢谢。

标签: mysql codeigniter sql-update batch-updates updatebatchsize


【解决方案1】:

追加到表字段t_franchise_price而不是覆盖。

更改:

$this - > franchise_price_model - > batchTestupdate($postData);

收件人:

$this->franchise_price_model->batchTestupdate($postData, 2);

把你的模型方法改成这样:

public function batchTestupdate($data = [], $method=1){
    $db_table = $this->db->dbprefix($this->table_test);
    $sql = "";

    if($method == 1){
        $this->db->update_batch($this->table_test, $data , 'test_id');
    }
    else if($method == 2){
        foreach($data as $k=>$v){
            $sql = "UPDATE $db_table SET t_franchise_price = TRIM(BOTH ',' FROM CONCAT(COALESCE(t_franchise_price,''), ',', ?)) WHERE test_id = ?;";
            $result = $this->db->query($sql, array($v['t_franchise_price'], $v['test_id']));
            if($result === false) {
                echo "ERROR at index $k - query: ((".$this->db->last_query()."))<br/><br/>\n\n";
                print_r($this->db->error()); echo "<br/><br/>\n\n";
            }
        }
    }
}

【讨论】:

  • @karlo Kokkak 先生,代码运行良好 |非常感谢|
  • 先生,这是我的更新问题,请您帮帮我先生。我真的不明白 。链接在这里 - -- stackoverflow.com/questions/50676133/…
  • 好的。我会尽量调查一下。
  • 谢谢先生。供您跟进。
猜你喜欢
  • 2017-04-06
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多