我在PHP框架的帮助下编码 - Laravel。此代码用于将图像上传到 Amazon S3 并将每个图像信息存储到 MySQL 表:
$created_at = new DateTime;
$aws_path = 'path/to/folder';
$files = Input::file('images');
$insert[] = array(); #Initialize array for mass inserting into table
foreach($files as $file) {
# Image data
$image_id = mt_rand(1000000000,9999999999);
$extension= $file->guessClientExtension();
$filename = $aws_path.'/'.$image_id.".".$extension;
$path = $file->getRealPath();
#UPLOAD IMAGE TO AMAZON S3
$obj = array(
'Bucket' => 's3.bucket.com',
'Key' => $filename,
'SourceFile' => $path,
'ACL' => 'public-read',
);
AWS::get('s3')->putObject($obj);
$insert[] = array(
'car_id' => $car_id,
'image_id' => $image_id,
'image_extension' => $extension,
'is_main' => true,
'created_at' => $created_at
);
}
DB::table('images')->insert($insert); #THIS LINE DOESN'T WORK
现在,这段代码 100% 有效,除了我无法将数据提交到我的表中,$insert 变量应该填充每个图像的数据,然后在 foreach 循环结束后,我将$insert上的数据"插入"到我的表中称为"images"。
我没有收到任何错误,它只是不起作用。为什么会这样?提前谢谢。
$insert
数组的第一个元素是一个空数组。
而不是像那样初始化$insert
(在第 4 行):
$insert[] = array();
试试:
$insert = array();
我相信
你正在添加一个额外的数组:
尝试。。。
$insert = array(
'car_id' => $car_id,
'image_id' => $image_id,
'image_extension' => $extension,
'is_main' => true,
'created_at' => $created_at
);
http://laravel.com/docs/4.2/queries
我刚刚发现我是个白痴(对不起)。
我的代码总是有效,但我从未意识到我能够在 PHPmyadmin 中更改表格的页面,并且我无法添加超过 30 条记录,但其他记录在其他页面中。因此,我总共有来自多个测试的 140 条记录。在这里浪费了2天的人,我的截止日期少了2天。感谢所有帮助你的人总是对的。