querybuilder数据库插入使用数组迭代作为列


laravel 5.3 querybuilder db insert using array iteration as column

我是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]                        
        );
    }  

如果我在关联视图上print_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 insert时:

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]: Column not found: 1054 Unknown Column '0' in 'field list' (SQL: insert into primary_votes (0, 1) values (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) values (?), (?), (?), (?), (??)",阵列(' 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 '),717年Connection.php线对象(关闭))运行('insert into primary_votes (0, 1) values (?), (?), (?), (?), (??)",阵列(' 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 '),481年Connection.php线对象(关闭))insert into primary_votes (0, 1) values (?)), (?), (?), (?), (??)",阵列(' 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 '))435年Connection.php线insert into primary_votes (0, 1) values (?)), (?), (?), (?), (??)",阵列(' 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 '))2117年Builder.php线at Builder->insert(array('member_user_id' => array('1', '1'), 'candidates_member_id' => array('12345', '54321'), 'candidates_district' => array('6', '6'), 'created_at' => array('2016-09-03 15:07:14', '2016-09-03 15:07:15'), 'updated_at' => array('2016-09-03 15:07:14', '2016-09-03 15:07:15'))) in VoteSubmitController.php第60行

所以,由于某种原因我无法弄清楚,它试图使用数组"行"键作为列名。为什么将单个行键解释为DB中的列?

你的代码应该是这样的:

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]                        
    );
    DB::table('primary_votes')->insert($arrData);
}

mysql错误的原因是$member_user_id, $candidates_member_id是数组而不是标量值。