[英]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.