簡體   English   中英

使用Codeigniter中的where子句更新多行

[英]Update multiple rows with where clause in codeigniter

我有一個數據庫記錄列表,請參閱附件。 如何使用學生ID為每條記錄插入增值稅號(請參見最左列)。 每個記錄都有一個輸入字段(請參見最右邊的列)。 在此處輸入圖片說明

這是我的看法:

    <?php $attributes = array('id' => 'VATformAdd', 'name' => 

'VATformAdd', 'autocomplete' => 'off'); echo form_open('report/addVATno', $attributes);?>
<fieldset>
<legend>Income By Student Admission</legend>
<table class="">
<thead>
<tr>
<th width="15%">Student ID</th>
<th width="15%">Student Form No</th>
<th width="15%">Course</th>
<th width="20%">Student Name</th>
<th width="15%">Admitted by</th>
<th width="10%">Amount</th>
<th width="10%">VAT Serial No</th>
</tr>
</thead>

<tbody>
<?php
    if (isset($addmission_income)) {
        foreach ($addmission_income as $addmissionincome) {
?>
<tr>
    <td><?php echo $addmissionincome->student_id;?></td>
    <td><?php echo $addmissionincome->student_form_no;?></td>
    <td><?php echo $addmissionincome->course_name;?></td>
    <td><?php echo $addmissionincome->student_full_name;?></td>
    <td><?php echo user_profile_name($addmissionincome->student_added_by); ?></td>
    <td><?php echo $addmissionincome->student_fee_paid;?></td>
    <td><input type="text" name="vatno[<?php echo $addmissionincome->student_id;?>]" class="width-100"/></td>
<?php       
        }
?>
</tr>
<?php       
    }
?>
</tbody>
</table>

<p>
<input id="submit" name="submit" type="submit"  value="Add VAT Number" />
</p>

</fieldset>
<?php echo form_close();?>

這是我的控制器:

function addVATno(){
        $studentid = $this->input->post('studentid');
        foreach($studentid as $a){
            if($val[$a]!=''){
                $this->report_mdl->addVATno($a);
            }
        }
    }

這是模型:

function addVATno($a){
        $val = $this->input->post('vatno');
        $updatevatsl = array( 
            'dupVATserial_no' => $val);
        $this->db->where('student_id', $a);
        $query = $this->db->update('data_student_master', $updatevatsl);
        return $query;
    }

使用隱藏的student_id來創建視圖文件,例如:

<input type="hidden" name="student_id[]" value="<?php echo $addmissionincome->student_id;?>"/>

vatno字段:

<input type="text" name="vatno[]" class="width-100"/>

將以下內容添加到您的控制器即可滿足您的所有需求(無需在模型中放置任何代碼):

  $student_id = $this->input->post('student_id');
  $vatno = $this->input->post('vatno');

  for ($i = 0; $i < count($student_id); $i++) {
        $sm_data[] = array(
            'vatno' => $vatno[$i],
            'student_id' => $student_id[$i]
        );
  }

$this->db->update_batch('data_student_master', $sm_data, 'student_id');

注意:最好將db調用移至模型以遵守MVC約定。

您的問題是您沒有從POST中提取正確的值。

您沒有在表單中的任何位置單獨提交學生證,因此我看不到如何從POST訪問它。

您還嘗試從POST訪問“ vatno”以將其插入數據庫,但是POST中的“ vatno”是一個數組。

因為您在表單中將學生ID用作數組鍵,所以您可以從同一數組中訪問要更新的學生ID和增值稅號。 在您的控制器中,您可以執行以下操作:

function addVATno(){
    //vatnos is an array, the key is the student id
    $vatnos = $this->input->post('vatno');
    foreach($vatnos as $key=>$vatno){
        $this->report_mdl->addVATno($key, $vatno);
    }
}

然后在模型中,您只需要:

function addVATno($studentid, $vatno){
    $updatevatsl = array( 
        'dupVATserial_no' => $vatno
    );
    $this->db->where('student_id', $studentid);
    $query = $this->db->update('data_student_master', $updatevatsl);
    return $query;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM