【发布时间】:2016-09-03 17:29:19
【问题描述】:
我是 Laravel 的新手,充其量是 PHP 的业余爱好者,但我正在努力学习。这是我的第一个问题,因为我通常能够找到答案。
这可能是我忽略的简单事情。我正在提交一个从视图到控制器最多可以包含 2 行的表单。我正在我的控制器中从数据中创建一个数组:
foreach($candidates_member_id as $key => $value)
{
$arrData[] = array(
'member_user_id' => $member_user_id[$key],
'candidates_member_id' => $candidates_member_id[$key],
'candidates_district' => $candidates_district[$key],
'candidate_name' => $candidate_name[$key],
'created_at' => $created_at[$key],
'updated_at' => $updated_at[$key]
);
}
如果我在关联视图上打印_r($arrData),我会得到这样的结果:
Array
(
[0] => Array
(
[member_user_id] => 1
[candidates_member_id] => 12345
[candidates_district] => 6
[candidate_name] => Doe, John
[created_at] => 2016-09-03 15:07:14
[updated_at] => 2016-09-03 15:07:14
)
[1] => Array
(
[member_user_id] => 1
[candidates_member_id] => 54321
[candidates_district] => 6
[candidate_name] => Doe, Jane
[created_at] => 2016-09-03 15:07:15
[updated_at] => 2016-09-03 15:07:15
)
)
但是,当我在上面的 foreach 循环中添加 DB 插入时:
DB::table('primary_votes')->insert(
['member_user_id' => $member_user_id,
'candidates_member_id' => $candidates_member_id,
'candidates_district' => $candidates_district,
'created_at' => $created_at,
'updated_at' => $updated_at]
);
我收到以下错误:
SQLSTATE[42S22]: 找不到列: 1054 '字段列表'中的未知列 '0' (SQL: 插入到primary_votes (0, 1) 值 (1, 1), (12345, 54321), (6, 6), (2016-09-03 15:07:14, 2016-09-03 15:07:15), (2016-09-03 15:07:14, 2016-09-03 15:07:15))
在 Connection.php 第 761 行
在 Connection->runQueryCallback('insert into primary_votes (0, 1) 值 (?, ?), (?, ?), (?, ?), (?, ?), (?, ? )', array('1', '1', '12345', '54321', '6', '6', '2016-09-03 15:07:14', '2016-09-03 15': Connection.php 第 717 行中的 07:15'、'2016-09-03 15:07:14'、'2016-09-03 15:07:15')、object(Closure))
在 Connection->run('insert into primary_votes (0, 1) 值 (?, ?), (?, ?), (?, ?), (?, ?), (?, ? )', array('1', '1', '12345', '54321', '6', '6', '2016-09-03 15:07:14', '2016-09-03 15': Connection.php 第 481 行中的 07:15'、'2016-09-03 15:07:14'、'2016-09-03 15:07:15')、object(Closure))
在 Connection->statement('insert into primary_votes (0, 1) 值 (?, ?), (?, ?), (?, ?), (?, ?), (?, ? )', array('1', '1', '12345', '54321', '6', '6', '2016-09-03 15:07:14', '2016-09-03 15': Connection.php 第 435 行中的 07:15'、'2016-09-03 15:07:14'、'2016-09-03 15:07:15'))
在 Connection->insert('insert into primary_votes (0, 1) 值 (?, ?), (?, ?), (?, ?), (?, ?), (?, ? )', array('1', '1', '12345', '54321', '6', '6', '2016-09-03 15:07:14', '2016-09-03 15': 07:15'、'2016-09-03 15:07:14'、'2016-09-03 15:07:15')) 在 Builder.php 行 2117
在 Builder->insert(array('member_user_id' => array('1', '1'), 'candidates_member_id' => array('12345', '54321'), 'candidates_district' => array('6' , '6'), 'created_at' => 数组('2016-09-03 15:07:14', '2016-09-03 15:07:15'), 'updated_at' => 数组('2016- 09-03 15:07:14', '2016-09-03 15:07:15'))) 在 VoteSubmitController.php 第 60 行
所以,由于某种原因,我无法弄清楚,它试图使用数组“行”键作为列名。为什么将单个行键解释为数据库中的列?提前致谢。
【问题讨论】:
标签: php arrays laravel-5.3