猿问

如何使用codeigniter更新表中的确切行

我的查看页面:

在这张图片中,我正在获取类似loan no, 的数据partyname,coll.amt使用date(即它显示了我给出时那个特定日期的数据,来自“集合”表)


如果我输入了loan no2的代表 amt,但我没有给loan no1、3。当我按下ok button代表 amt 时,应该在该“集合”表中更新确切的贷款编号和日期。


我的型号代码:


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>


请解决这个问题。


元芳怎么了
浏览 109回答 1
1回答

HUX布斯

如果您只想对ramt[]具有非空值的输入进行更新,您可以为每个输入文本名称分配一个键,以便稍后识别它:<table class="table table-bordered table-striped table-xxs" id="tb3">&nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; <th>Loan No</th>&nbsp; &nbsp; <th>Party Name</th>&nbsp; &nbsp; <th>Coll.Amt</th>&nbsp; &nbsp; <th>Rep Amt</th>&nbsp; &nbsp; </tr>&nbsp; &nbsp; </thead>&nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; //echo '<pre>';print_r($result2);exit();&nbsp; &nbsp; &nbsp; &nbsp; if(!empty($query)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($query as $key => $row){ // added $key as index&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td ><input style="width:50px" type="text" class="form-control input-xs" name="Sno[<?php echo $key ?>]" id="Sno" value="<?=$row['loanno'];?>"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td> <input style="width:180px" type="text" class="form-control input-xs" name="name[<?php echo $key ?>]" id="Amount" value="<?=$row['pname'];?>"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }?>&nbsp;&nbsp; &nbsp; </tbody></table>并过滤您的batchinsert()函数的输入数据:public function batchinsert($data){&nbsp; &nbsp; $session_data = $this->session->userdata('logged_in');&nbsp; &nbsp; $data['username'] = $session_data['repname'];&nbsp; &nbsp; $LDate = $this->input->post('CDate');&nbsp; &nbsp; $date = str_replace('/', '-', $LDate);&nbsp; &nbsp; $newDate = date("Y-m-d", strtotime($date));&nbsp; &nbsp; $lno = $this->input->post("Sno");&nbsp; &nbsp; $ramt = $this->input->post("ramt"); // added ramt input variable&nbsp; &nbsp; $count = count($data['Sno']);&nbsp; &nbsp; $updateArray = array();&nbsp; &nbsp; for($x = 0; $x < sizeof($lno); $x++){&nbsp; &nbsp; &nbsp; &nbsp; if (!empty($ramt[$x])) { // this will only insert data on loanno which have non-empty ramt values&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $updateArray[] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'loanno' => $lno[$x],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'receive_amt'=> $ramt[$x]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; $this->db->where('collection_date',$newDate);&nbsp; &nbsp; $this->db->update_batch('collection',$updateArray,'loanno');&nbsp; &nbsp; //&nbsp; &nbsp; &nbsp; &nbsp; $this->db->insert_batch('test', $entries2);&nbsp; &nbsp; redirect('Collection_Entry','refresh');}这只会遍历非空ramt[]输入,然后只更新动态loanno值和静态collection_date值。// Example query output :&nbsp;// UPDATE `collection`// SET// `receive_amt` =&nbsp;// CASE&nbsp;//&nbsp; &nbsp; &nbsp;WHEN `loanno` = '1' THEN 1&nbsp;//&nbsp; &nbsp; &nbsp;WHEN `loanno` = '3' THEN 2&nbsp;//&nbsp; &nbsp; &nbsp;WHEN `loanno` = '6' THEN 3&nbsp;//&nbsp; &nbsp; &nbsp;WHEN `loanno` = '17' THEN 4&nbsp;//&nbsp; &nbsp; &nbsp;ELSE `receive_amt`// END&nbsp;// WHERE `collection_date` = '2019/01/09' AND `loanno` IN ('1','3','6','17')
随时随地看视频慕课网APP
我要回答