PHP数组传递至MySQL表时遇错误,请求排查解决
问题描述
我尝试通过PHP数组实现主从表概念的数据传递至MySQL表,详情部分的控制器代码如下:
if ($this->form_validation->run() == true) { $count = count($this->input->post('vote')); $votes = $this->input->post('vote'); $amount = $this->input->post('amount'); $notes = $this->input->post('notes'); for ($x = 0; $x < $count; $x++) { $details[$x]['voucher_id'] = $id; $details[$x]['vote'] = $votes[$x]; $details[$x]['amount'] = $amount[$x]; $details[$x]['notes'] = $notes[$x]; $details[$x]['status'] = 1; } } if ($this->Voucher_model->updateVoteDetailsById($details)) { $this->session->set_flashdata('message', 'Votes are added Successfully ..!!'); }
对应的模型代码如下:
public function updateVoteDetailsById($details) { $this->db2->insert_batch('tbl_voucher_votes', $details); if ($this->db2->affected_rows() == true) { foreach ($details as $array) { } return true; } }
目标表tbl_voucher_votes包含voucher_vote_id、voucher_id、vote、amount、notes、status字段,期望插入结果如下:
+-----------------+------------+------+--------+-------+--------+ | voucher_vote_id | voucher_id | vote | amount | notes | status | +-----------------+------------+------+--------+-------+--------+ | 1 | 2 | 16 | 14500 | | 1 | | 2 | 4 | 18 | 65900 | | 1 | | 3 | 3 | 12 | 26200 | | 1 | +-----------------+------------+------+--------+-------+--------+
执行代码时出现以下错误:
A PHP Error was encountered Severity: Notice Message: Undefined variable: details Filename: controllers/Voucher.php Line Number: 466 Backtrace: File: C:\xampp\htdocs\cssams\application\controllers\Voucher.php Line: 466 Function: _error_handler File: C:\xampp\htdocs\cssams\index.php Line: 269 Function: require_once A Database Error Occurred You must use the "set" method to update an entry. Filename: C:/xampp/htdocs/cssams/application/models/Voucher_model.php Line Number: 109
错误原因分析
- Undefined variable: details:
$details仅在表单验证通过的分支内定义,当表单验证失败时,$details未初始化就被传入模型方法,触发PHP未定义变量警告。 - 数据库错误"You must use the 'set' method to update an entry":
模型方法名是updateVoteDetailsById(带有update语义),但实际执行的是insert_batch插入操作。若该方法之前是用于更新逻辑,可能遗留了未清理的更新代码;另外,当$details为空数组时,insert_batch执行也会触发异常。
解决方案
1. 初始化变量并增加空值判断
在控制器开头初始化$details为空数组,避免未定义问题,同时在调用模型前检查数据是否有效:
// 初始化空数组,确保变量始终存在 $details = []; if ($this->form_validation->run() == true) { $count = count($this->input->post('vote')); $votes = $this->input->post('vote'); $amount = $this->input->post('amount'); $notes = $this->input->post('notes'); for ($x = 0; $x < $count; $x++) { $details[$x]['voucher_id'] = $id; $details[$x]['vote'] = $votes[$x]; $details[$x]['amount'] = $amount[$x]; $details[$x]['notes'] = $notes[$x]; $details[$x]['status'] = 1; } } // 先判断数据非空再调用模型 if (!empty($details) && $this->Voucher_model->insertVoteDetails($details)) { $this->session->set_flashdata('message', 'Votes are added Successfully ..!!'); }
2. 修正模型方法逻辑
修改方法名使其与功能匹配,移除无用代码并增加空数据判断:
// 方法名改为insertVoteDetails,明确是插入操作 public function insertVoteDetails($details) { // 空数据直接返回失败 if (empty($details)) { return false; } $this->db2->insert_batch('tbl_voucher_votes', $details); // 受影响行数大于0则返回成功 return $this->db2->affected_rows() > 0; }
3. 额外检查项
- 确认
$id变量已正确赋值,否则voucher_id会插入空值; - 确保表单中的
vote、amount等字段是数组格式,比如输入框命名为vote[]、amount[]; - 验证模型中的
$db2是正确的数据库连接实例。
内容的提问来源于stack exchange,提问作者MCIT Trends
相关产品推荐
相关产品推荐

