我有一個簡單的codeigniter表單,其中有一個復選框,如下所示:
。 <th>/span> < input class="acb"/span> type="checkbox" name="generateawb[]" value="< ? =$val->id? >" onclick="deRequire('acb')" required /> </th>>
< button type="submit" name="savemedaa"/span> style="margin-left: 5px; font-size: 0. 6em;" class="btn btn-warning">GENERATE AWB</button>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
因此,當用戶選擇一個或多個復選框時,它應該把選擇的資料插入到另一個表中,我的控制器如下:
public function generateawb() {
$data = array()。
如果(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)。
}
}
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
我的模型如下:
public function selectawb($checkboxs) {
$this->db->select('*')。
$this->db->where_in("id", $checkboxs);
$this->db->from('fileupload');
$query = $this->db->get();
$result = $query->result();
回傳$result。
}
函式 generateawb($data){
$this->db->insert_batch('consignments', $data)。
}
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
現在的問題是,即使用戶選擇了多個復選框,也只有一個復選框被保存到資料庫中,誰能告訴我這里面有什么問題,謝謝
uj5u.com熱心網友回復:你把 $data[] 放在 foreach 之外,所以它只有一個值。只要把它放在里面:
public function generateawb()
{
$data = array() 。
if (isset($_POST['savemedaa'/span>])) {
$checkboxs = $_POST['generateawb'] 。
$generateawb = $this->excel_import_model->selectawb($checkboxs)。
foreach ($generateawb as$row) {
$clientid = $row-> id;
$batchnumber = $row->batchnumber。
$awbg = random_int(100000000, 999999)。
$awb = 'LRG'/span> . $awbg;
$data[] = array(
'clientid' => $clientid,
'awb' => $awb,
'batchnumber' => $batchnumber,
);
}
$this->excel_import_model-> generateawb($data);
}
你應該使用一個格式化器來避免這樣的錯誤。如果你使用VSCode,你可以使用這個extention
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/311017.html
標籤:
