在BD Laravel中插入数组

时间:2015-08-13 17:17:08

标签: php arrays laravel

我想在BD

中添加数组数据

我有这段代码

$id=$usuario->id;
$datereg = date ( "Y-m-d H:i:s" );
$dateact = date ( "Y-m-d H:i:s" );
$idcat = array($request->input ( 'cat' ));
$sqlInsert = array(
    array('id'=>$id, 'idcat'=>$idcat, 'datereg'=>$datereg,'dateact'=>$dateact),
);

//DB::statement ( "INSERT INTO intereses (usuario_movil_id,categorias_id, fecha_registro, fecha_actualizacion) VALUES ('$id', '$idcat','$datereg','$dateact')" );

foreach ( $sqlInsert as $row ) {

    DB::statement(
            "INSERT INTO intereses (usuarios_movil_id,categorias_id, fecha_registro, fecha_actualizacion) VALUES (? , ? , ? , ? )",
            array($row['id'], $row['idcat'], $row['datereg'], $row['dateact'])
    );

}

此代码向我显示错误" preg_replace():参数不匹配,pattern是一个字符串,而replacement是一个数组"

任何想法?

1 个答案:

答案 0 :(得分:0)

我修改了代码

$id=$usuario->id;
            $datereg = date ( "Y-m-d H:i:s" );
            $dateact = date ( "Y-m-d H:i:s" );
            $idcat = array($request->input ( 'cat' ));
            $sqlInsert = 
                    array('idcat'=>$idcat);
                //DB::statement ( "INSERT INTO intereses (usuario_movil_id,categorias_id, fecha_registro, fecha_actualizacion) VALUES ('$id', '$idcat','$datereg','$dateact')" );

                foreach ( $idcat as $row=>$value ) {

                    DB::statement(
                            "INSERT INTO intereses (usuarios_movil_id,categorias_id, fecha_registro, fecha_actualizacion) VALUES ('$id', '".$row[$request->input ( 'cat' )]."', '$datereg', '$dateact' )"
                            //array($id, $row['idcat'], $datereg, $dateact)
                    );

                }

现在'' categorias_id''它空了