codeigniter 将多个值插入数据库不起作用
codigniter inserting multiple values to database not working
我有一个简单的 codeigniter 表单,其中有一个如下所示的复选框:
<th> <input class="acb" type="checkbox" name="generateawb[]" value="<?=$val->id?>" onclick="deRequire('acb')" required /></th>
<button type="submit" name="savemedaa" style="margin-left:5px; font-size: 0.6em; " class="btn btn-warning">GENERATE AWB</button>
所以当用户选择一个或多个复选框时,它应该将所选数据插入另一个 table,我的控制器如下所示:
public function generateawb() {
$data = array();
if(isset($_POST['savemedaa']))
{
$checkboxs = $_POST['generateawb'];
$generateawb = $this->excel_import_model->selectawb($checkboxs);
foreach($generateawb as $row){
$clientid= $row->id;
$batchnumber= $row->batchnumber;
}
$awbg = random_int(100000000, 999999999);
$awb='LRG'.$awbg;
$data[] = array(
'clientid'=>$clientid,
'awb'=>$awb,
'batchnumber'=>$batchnumber,
);
$this->excel_import_model->generateawb($data);
}
}
}
我的模型如下:
public function selectawb($checkboxs) {
$this->db->select('*');
$this->db->where_in("id", $checkboxs);
$this->db->from('fileupload');
$query = $this->db->get();
$result = $query->result();
return $result;
}
function generateawb($data){
$this->db->insert_batch('consignments', $data);
}
现在的问题是,即使用户选择了多个复选框,也只有一个复选框被保存到数据库中,谁能告诉我这里出了什么问题,谢谢
您将 $data[] 放在 foreach 之外,因此它只有一个值。把它放在里面:
public function generateawb()
{
$data = array();
if (isset($_POST['savemedaa'])) {
$checkboxs = $_POST['generateawb'];
$generateawb = $this->excel_import_model->selectawb($checkboxs);
foreach ($generateawb as $row) {
$clientid = $row->id;
$batchnumber = $row->batchnumber;
$awbg = random_int(100000000, 999999999);
$awb = 'LRG' . $awbg;
$data[] = array(
'clientid' => $clientid,
'awb' => $awb,
'batchnumber' => $batchnumber,
);
}
$this->excel_import_model->generateawb($data);
}
}
您应该使用格式化程序来避免此类错误。如果你使用 VSCode 你可以使用这个 extention
我有一个简单的 codeigniter 表单,其中有一个如下所示的复选框:
<th> <input class="acb" type="checkbox" name="generateawb[]" value="<?=$val->id?>" onclick="deRequire('acb')" required /></th>
<button type="submit" name="savemedaa" style="margin-left:5px; font-size: 0.6em; " class="btn btn-warning">GENERATE AWB</button>
所以当用户选择一个或多个复选框时,它应该将所选数据插入另一个 table,我的控制器如下所示:
public function generateawb() {
$data = array();
if(isset($_POST['savemedaa']))
{
$checkboxs = $_POST['generateawb'];
$generateawb = $this->excel_import_model->selectawb($checkboxs);
foreach($generateawb as $row){
$clientid= $row->id;
$batchnumber= $row->batchnumber;
}
$awbg = random_int(100000000, 999999999);
$awb='LRG'.$awbg;
$data[] = array(
'clientid'=>$clientid,
'awb'=>$awb,
'batchnumber'=>$batchnumber,
);
$this->excel_import_model->generateawb($data);
}
}
}
我的模型如下:
public function selectawb($checkboxs) {
$this->db->select('*');
$this->db->where_in("id", $checkboxs);
$this->db->from('fileupload');
$query = $this->db->get();
$result = $query->result();
return $result;
}
function generateawb($data){
$this->db->insert_batch('consignments', $data);
}
现在的问题是,即使用户选择了多个复选框,也只有一个复选框被保存到数据库中,谁能告诉我这里出了什么问题,谢谢
您将 $data[] 放在 foreach 之外,因此它只有一个值。把它放在里面:
public function generateawb()
{
$data = array();
if (isset($_POST['savemedaa'])) {
$checkboxs = $_POST['generateawb'];
$generateawb = $this->excel_import_model->selectawb($checkboxs);
foreach ($generateawb as $row) {
$clientid = $row->id;
$batchnumber = $row->batchnumber;
$awbg = random_int(100000000, 999999999);
$awb = 'LRG' . $awbg;
$data[] = array(
'clientid' => $clientid,
'awb' => $awb,
'batchnumber' => $batchnumber,
);
}
$this->excel_import_model->generateawb($data);
}
}
您应该使用格式化程序来避免此类错误。如果你使用 VSCode 你可以使用这个 extention