
本文讲解如何从数据库查询结果数组中,依据用户在表单中选择的 id(而非固定取 [0]),动态获取对应对象的 inventory_update_stock_details_id 值,并正确传递给后续业务逻辑。
本文讲解如何从数据库查询结果数组中,依据用户在表单中选择的 id(而非固定取 [0]),动态获取对应对象的 inventory_update_stock_details_id 值,并正确传递给后续业务逻辑。
在您当前的代码中,关键问题在于这行硬编码逻辑:
$id2 = $locationProducts[0]->inventory_update_stock_details_id;
它始终取数组第一个元素的 ID,导致无论用户在下拉菜单中选择哪个 inventory_update_stock_details_id(如 35、345、2),系统都只使用 36 —— 这显然违背了表单交互的本意。
✅ 正确做法:从表单输入中获取用户真实选择的 ID
您的 View 已正确渲染了
<option value="<?php echo $row->inventory_update_stock_details_id; ?>"> <?php echo $row->inventory_update_stock_details_id . ' - ' . $row->inventory_item_name; ?> </option>
因此,在 Controller 中,您应通过 $_POST(或 CodeIgniter 的 $this->input->post())直接读取用户提交的选中值,而不是遍历整个 $locationProducts 数组:
// ✅ 正确:获取用户在表单中实际选择的 inventory0 的值
$selected_id = $this->input->post('inventory0');
if (empty($selected_id) || !is_numeric($selected_id)) {
$this->session->set_flashdata('error', '请选择有效的库存明细项。');
$this->render('item/addInventoryLocation', $meta, $this->data);
return;
}
$id2 = (int) $selected_id;
? 注意:inventory0 是您 HTML 中
? 扩展:支持多行动态表单(推荐结构化写法)
如果您的界面允许用户添加多行物料(通过 JS 动态增加 inventory1, inventory2 等字段),建议改用数组命名方式并统一处理:
View 修改(示例):
<select name="inventory_ids[]" class="form-control select2" required><option value="">请选择...</option>
<?php foreach ($locationProducts as $row): ?><option value="<?= $row->inventory_update_stock_details_id ?>">= $row->inventory_update_stock_details_id ?> - = htmlspecialchars($row->inventory_item_name) ?>
</option>
<?php endforeach; ?></select>
Controller 对应逻辑:
$selected_ids = $this->input->post('inventory_ids');
if (!is_array($selected_ids) || empty($selected_ids)) {
$this->session->set_flashdata('error', '至少需选择一项库存明细。');
$this->render('item/addInventoryLocation', $meta, $this->data);
return;
}
$details = [];
foreach ($selected_ids as $idx => $id2) {
if (!is_numeric($id2)) continue;
// 构建每条明细数据(注意:serial_no、item_code 等也应使用 idx 匹配)
$details[] = [
'inventory_update_stock_id' => (int)$id2,
'inventory' => $this->input->post("inventory{$idx}"),
'serial_no' => $this->input->post("serial_no{$idx}"),
'item_code' => $this->input->post("item_code{$idx}"),
'officer' => $this->input->post("officer{$idx}"),
'qty' => 1,
'status' => 1,
'branch' => $this->input->post('branch'),
];
}
if (!empty($details) && $this->Item_model->addInventoryLocationDetails($details)) {
$this->session->set_flashdata('message', '成功添加 ' . count($details) . ' 条定位明细!');
redirect('item/addInventoryLocation');
} else {
$this->session->set_flashdata('error', '保存失败,请检查数据。');
$this->render('item/addInventoryLocation', $meta, $this->data);
}
⚠️ 重要注意事项
- 安全过滤:始终对 $_POST 数据进行类型校验与转义(如 (int) 强制转换、htmlspecialchars() 输出防 XSS);
- 空值校验:避免未选中时 $id2 为 null 或空字符串导致 SQL 错误;
- 数据库一致性:确保 inventory_update_stock_details_id 在 store_inventory_update_stock_details 表中真实存在且状态有效(您原查询已含 status=1 和 NOT IN 排除逻辑,合理);
- 调试技巧:使用 var_dump($this->input->post()); 快速确认实际提交的字段名与值;
- CodeIgniter 版本兼容性:$this->input->post() 在 CI3/CI4 中行为一致,但 CI4 推荐使用 service('request') 替代。
✅ 总结
根本解决方案不是“遍历所有 $locationProducts”,而是信任表单提交的数据源——用户在
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











