【问题标题】:How to update the exact row in table using codeigniter如何使用 codeigniter 更新表中的确切行
【发布时间】:2019-07-13 09:14:42
【问题描述】:

我的浏览页面:

在这张图片中,我使用date 获取loan nopartynamecoll.amt 之类的数据(即,它显示我给出的特定日期的数据,来自“收集”表)

如果我输入了loan no 2 的rep amt 但我没有给loan no 1、3。当我按下ok button 时,rep amt 应该在那个“collecion”表中更新以获得确切的贷款编号和日期。

我的型号代码:

public function batchinsert($data){
    $session_data = $this->session->userdata('logged_in');
    $data['username'] = $session_data['repname'];
    $LDate = $this->input->post('CDate');
    $date = str_replace('/', '-', $LDate);
    $newDate = date("Y-m-d", strtotime($date));
    $lno = $this->input->post("Sno");
    $count = count($data['Sno']);
    for($i = 0; $i<$count; $i++){ 
        $entries2[] = array(               
            'receive_amt'=>$data['ramt'][$i],

            ); 
    }
    $this->db->where('loanno',$lno);
    $this->db->where('collection_date',$newDate);
    $this->db->update_batch('collection',$entries2);
    //        $this->db->insert_batch('test', $entries2);
    redirect('Collection_Entry','refresh');
}

我的控制器代码:

public function Collection_Insert(){
    $this->load->model('User_model');
    $result = $this->User_model->batchinsert($_POST);
}

我的查看页面代码:

<table class="table table-bordered table-striped table-xxs" id="tb3">
    <thead>
        <tr>
            <th>Loan No</th>
            <th>Party Name</th>
            <th>Coll.Amt</th>
            <th>Rep Amt</th>
        </tr>
    </thead>
    <tbody>
    <?php
    //echo '<pre>';print_r($result2);exit();
    if(!empty($query)){
        foreach($query as $row){
    ?>
        <tr >
        <td ><input style="width:50px" type="text" class="form-control input-xs" name="Sno[]" id="Sno" value="<?=$row['loanno'];?>"></td>

        <td> <input style="width:180px" type="text" class="form-control input-xs" name="name[]" id="Amount" value="<?=$row['pname'];?>"></td>

        <td ><input style="width:80px" type="text" class="form-control input-xs amt" name="Amount[]" id="Bankname" value="<?=$row['collection_amt'];?>"></td>

        <td ><input style="width:80px" type="text" class="form-control input-xs ramt" name="ramt[]" id="Chqamt" value="<?=$row['receive_amt'];?>" autofocus></td>
        </tr>

    <?php
        }
    }?> 
    </tbody>
</table>

请解决这个问题。

【问题讨论】:

  • 您能发布您的表单视图吗?还有你从哪里得到这些 $data 变量的?
  • 为了识别您的特定行,您需要为不同的输入提供不同的类。
  • @ArifulIslam 请查看我编辑的代码然后清除它

标签: php codeigniter row


【解决方案1】:

如果您只想对具有非空值的ramt[] 输入进行更新,您可以为每个输入文本名称分配一个键以便稍后识别它:

<table class="table table-bordered table-striped table-xxs" id="tb3">
    <thead>
            <tr>
    <th>Loan No</th>
    <th>Party Name</th>
    <th>Coll.Amt</th>
    <th>Rep Amt</th>
    </tr>
    </thead>
    <tbody>
        <?php
        //echo '<pre>';print_r($result2);exit();
        if(!empty($query)){
            foreach($query as $key => $row){ // added $key as index
            ?>
                <tr >
                    <td ><input style="width:50px" type="text" class="form-control input-xs" name="Sno[<?php echo $key ?>]" id="Sno" value="<?=$row['loanno'];?>"></td>

                    <td> <input style="width:180px" type="text" class="form-control input-xs" name="name[<?php echo $key ?>]" id="Amount" value="<?=$row['pname'];?>"></td>

                    <td ><input style="width:80px" type="text" class="form-control input-xs amt" name="Amount[<?php echo $key ?>]" id="Bankname" value="<?=$row['collection_amt'];?>"></td>

                    <td ><input style="width:80px" type="text" class="form-control input-xs ramt" name="ramt[<?php echo $key ?>]" id="Chqamt" value="<?=$row['receive_amt'];?>" autofocus></td>
                </tr>

            <?php
            }
        }?> 
    </tbody>
</table>

并在batchinsert() 函数中过滤输入数据:

public function batchinsert($data){
    $session_data = $this->session->userdata('logged_in');
    $data['username'] = $session_data['repname'];
    $LDate = $this->input->post('CDate');
    $date = str_replace('/', '-', $LDate);
    $newDate = date("Y-m-d", strtotime($date));
    $lno = $this->input->post("Sno");
    $ramt = $this->input->post("ramt"); // added ramt input variable
    $count = count($data['Sno']);
    $updateArray = array();
    for($x = 0; $x < sizeof($lno); $x++){

        if (!empty($ramt[$x])) { // this will only insert data on loanno which have non-empty ramt values
            $updateArray[] = array(
                'loanno' => $lno[$x],
                'receive_amt'=> $ramt[$x]
            );
        }
    }
    $this->db->where('collection_date',$newDate);
    $this->db->update_batch('collection',$updateArray,'loanno');
    //        $this->db->insert_batch('test', $entries2);
    redirect('Collection_Entry','refresh');
}

这将仅循环通过非空 ramt[] 输入,然后仅更新动态 loanno 值和静态 collection_date 值。

// Example query output : 
// UPDATE `collection`
// SET
// `receive_amt` = 
// CASE 
//     WHEN `loanno` = '1' THEN 1 
//     WHEN `loanno` = '3' THEN 2 
//     WHEN `loanno` = '6' THEN 3 
//     WHEN `loanno` = '17' THEN 4 
//     ELSE `receive_amt`
// END 
// WHERE `collection_date` = '2019/01/09' AND `loanno` IN ('1','3','6','17')

【讨论】:

  • 这行显示错误 $this->db->update_batch('collection',$updateArray,'loanno');作为未定义的索引:loanno
  • 我已经打印并看到它在您的代码中的值是正确的,但是这一行 "$this->db->update_batch('collection',$updateArray,'loanno');"显示错误并且不更新值
  • 请问您使用的是哪个 CI 版本?
  • 2.1版本可能是版本问题没有来,我也以为这样只是版本问题
  • 我相信 update_batch 上的 undefined index 是 CI 版本 3.1.1 / 3.1.2 上的 known bug,我正在 CI 版本 3.1.5 上对其进行测试,它运行良好,不过我不知道 CI 2.1 版...
猜你喜欢
  • 2011-12-30
  • 2015-06-10
  • 1970-01-01
  • 2015-09-29
  • 2013-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多