Codeigniter 问题 update_batch
Problems with Codeigniter update_batch
我有一个视图文件,其中我根据数据库获取操作生成了一些输入字段。我正在尝试为参加考试的学生打分。这些学生是动态填充的,这里是视图文件的代码。
查看
<?php if($this->session->flashdata('info')) { ?>
<div class="alert alert-info">
<?php echo $this->session->flashdata('info'); ?>
</div>
<?php } ?>
<?php echo form_open('dashboard/insert_marks'); ?>
<?php foreach($entries as $ent) { ?>
<div class="row">
<div class="col-2">
<div class="form-group">
<label><?php echo ($ent->id); ?></label>
<input type="text" name="id" class="form-control" placeholder="N/A" disabled>
</div>
</div>
<div class="col-10">
<div class="form-group">
<label><?php echo ($ent->student_name); ?></label>
<input type="text" name="marks" class="form-control">
</div>
</div>
</div>
<?php } ?>
<button type="submit" name="save" value="Save Data" class="btn btn-primary">Submit</button>
<?php echo form_close(); ?>
控制器
在控制器中,我试图创建一个数组数组,以便我可以在 Codeigniter update_batch 函数的帮助下一次插入所有这些值。我在下面粘贴了控制器文件的代码。
public function insert_marks() {
$this->load->model('Marks_Model');
if($this->input->post('save'))
{
$idlist[] = $this->input->post('id');
$marklist[] = $this->input->post('marks');
$marks_data = array(array());
for($x = 0; $x < sizeof($idlist); $x++){
$marks_data[] = array(
'id' => $idlist[$x],
'marks' => $marklist[$x]
);
}
$this->Marks_Model->update_marks($marks_data);
$this->session->set_flashdata('info','Marks Updated Successfully');
redirect($_SERVER['HTTP_REFERER'],'refresh');
}
型号
在模型中,我调用了 update_batch 以插入数组中的所有 dtaa。这是我第一次使用update_batch.
public function update_marks($marks_data) {
$this->db->update_batch('marks',$marks_data,'id');
}
错误
我有一个错误"One or more rows submitted for batch updating is missing the specified index."
我不知道出了什么问题。感谢任何帮助。
输入已经是数组,所以使用 $idlist =
而不是 $idlist[] =
,$marklist
也是如此。这可能是 "missing the specified index" 错误的原因。
$marks_data
应该只是一个数组,而不是数组中的数组。这也可能是错误的原因。
$marks_data
的创建使用 foreach
循环编写(和阅读,IMO)稍微容易一些。
把这些放在一起,你就得到了这个。
public function insert_marks()
{
$this->load->model('Marks_Model');
if($this->input->post('save'))
{
$idlist = $this->input->post('id');
$marklist = $this->input->post('marks');
$marks_data = array();
foreach($idlist as $key => $id)
{
// we are filling an array with arrays here
$marks_data[] = array('id' => $id, 'marks' => $marklist[$key]);
}
$this->Marks_Model->update_marks($marks_data);
$this->session->set_flashdata('info', 'Marks Updated Successfully');
redirect($_SERVER['HTTP_REFERER'], 'refresh');
}
}
还需要进行一项更改。视图代码 foreach($entries as $ent)
告诉我们可能有不止一对 id
/marks
输入。这意味着输入名称必须是数组,因此请如下所示进行更改。
// Change this
<input type="text" name="id" ...
// to this
<input type="text" name="id[]" ...
对 <input type="text" name="marks"
执行相同操作,将其更改为 <input type="text" name="marks[]"
。此更改确保 $idlist
和 $marklist
将是数组,如果 foreach
起作用,这是必需的。
要确保它 "work" 需要为此方法编写更多代码。应进行检查以确认提供了值。然后,在将这些值插入数据库之前,应该对这些值进行验证和清理(永远不要相信用户输入)。
我有一个视图文件,其中我根据数据库获取操作生成了一些输入字段。我正在尝试为参加考试的学生打分。这些学生是动态填充的,这里是视图文件的代码。
查看
<?php if($this->session->flashdata('info')) { ?>
<div class="alert alert-info">
<?php echo $this->session->flashdata('info'); ?>
</div>
<?php } ?>
<?php echo form_open('dashboard/insert_marks'); ?>
<?php foreach($entries as $ent) { ?>
<div class="row">
<div class="col-2">
<div class="form-group">
<label><?php echo ($ent->id); ?></label>
<input type="text" name="id" class="form-control" placeholder="N/A" disabled>
</div>
</div>
<div class="col-10">
<div class="form-group">
<label><?php echo ($ent->student_name); ?></label>
<input type="text" name="marks" class="form-control">
</div>
</div>
</div>
<?php } ?>
<button type="submit" name="save" value="Save Data" class="btn btn-primary">Submit</button>
<?php echo form_close(); ?>
控制器
在控制器中,我试图创建一个数组数组,以便我可以在 Codeigniter update_batch 函数的帮助下一次插入所有这些值。我在下面粘贴了控制器文件的代码。
public function insert_marks() {
$this->load->model('Marks_Model');
if($this->input->post('save'))
{
$idlist[] = $this->input->post('id');
$marklist[] = $this->input->post('marks');
$marks_data = array(array());
for($x = 0; $x < sizeof($idlist); $x++){
$marks_data[] = array(
'id' => $idlist[$x],
'marks' => $marklist[$x]
);
}
$this->Marks_Model->update_marks($marks_data);
$this->session->set_flashdata('info','Marks Updated Successfully');
redirect($_SERVER['HTTP_REFERER'],'refresh');
}
型号 在模型中,我调用了 update_batch 以插入数组中的所有 dtaa。这是我第一次使用update_batch.
public function update_marks($marks_data) {
$this->db->update_batch('marks',$marks_data,'id');
}
错误
我有一个错误"One or more rows submitted for batch updating is missing the specified index."
我不知道出了什么问题。感谢任何帮助。
输入已经是数组,所以使用 $idlist =
而不是 $idlist[] =
,$marklist
也是如此。这可能是 "missing the specified index" 错误的原因。
$marks_data
应该只是一个数组,而不是数组中的数组。这也可能是错误的原因。
$marks_data
的创建使用 foreach
循环编写(和阅读,IMO)稍微容易一些。
把这些放在一起,你就得到了这个。
public function insert_marks()
{
$this->load->model('Marks_Model');
if($this->input->post('save'))
{
$idlist = $this->input->post('id');
$marklist = $this->input->post('marks');
$marks_data = array();
foreach($idlist as $key => $id)
{
// we are filling an array with arrays here
$marks_data[] = array('id' => $id, 'marks' => $marklist[$key]);
}
$this->Marks_Model->update_marks($marks_data);
$this->session->set_flashdata('info', 'Marks Updated Successfully');
redirect($_SERVER['HTTP_REFERER'], 'refresh');
}
}
还需要进行一项更改。视图代码 foreach($entries as $ent)
告诉我们可能有不止一对 id
/marks
输入。这意味着输入名称必须是数组,因此请如下所示进行更改。
// Change this
<input type="text" name="id" ...
// to this
<input type="text" name="id[]" ...
对 <input type="text" name="marks"
执行相同操作,将其更改为 <input type="text" name="marks[]"
。此更改确保 $idlist
和 $marklist
将是数组,如果 foreach
起作用,这是必需的。
要确保它 "work" 需要为此方法编写更多代码。应进行检查以确认提供了值。然后,在将这些值插入数据库之前,应该对这些值进行验证和清理(永远不要相信用户输入)。