在 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;
}
您的问题是您没有从 POST 中提取正确的值。
您没有在表单中的任何地方单独提交学生 ID,所以我看不出您如何从 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;
}
使用隐藏的 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');
注意:最好将数据库调用移动到您的模型以遵守 MVC 约定。
我有一个来自数据库的记录列表,见附件。我如何使用学生 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;
}
您的问题是您没有从 POST 中提取正确的值。
您没有在表单中的任何地方单独提交学生 ID,所以我看不出您如何从 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;
}
使用隐藏的 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');
注意:最好将数据库调用移动到您的模型以遵守 MVC 约定。