我想使用PHP MYSQL在同一行上传多个图像名称

时间:2015-06-25 15:32:37

标签: php mysql

这是我的代码

for ($i = 0; $i < count($_FILES["user_files"]["name"]); $i++) 
{
    // get the image mime type
    $image_mime = strtolower(image_type_to_mime_type(exif_imagetype($_FILES["user_files"]["tmp_name"][$i])));

    if (in_array($image_mime, $valid_image_check)) 
    {
        $folderName = "uploads/";
        $ext = explode("/", strtolower($image_mime));
        $ext = strtolower(end($ext));
        $filename = rand(10000, 990000) . '_' . time() . '.' . $ext;

        // if user upload a file abc,jpg, it will convert it to 291905_1399918178.jpg based on random number and time.
        $filepath = $folderName . $filename;
        if (!move_uploaded_file($_FILES["user_files"]["tmp_name"][$i], $filepath)) 
        {
              echo "fail uplaod";
        } else {
            $smsg .= "<strong>" . $_FILES["user_files"]["name"][$i] . "</strong> uploaded successfully. <br>";

            $magicianObj = new imageLib($filepath);
            $magicianObj->resizeImage(100, 100);
            $magicianObj->saveImage($folderName . 'thumb/' . $filename, 100);
        }

    } else {
        echo "not image";

    }
}

和MySQL查询是

$sql =  "INSERT INTO properties 
           (agent_id, property_name, category, location, 
            property_type, search_radius, price, bed_rooms, 
            bath_rooms, commercial_type, area, address, 
            description, image_name, date_added)
         VALUES 
           ('$agent_id', '$property_name', '$listing_for', '$city', 
            '$property_type', '$area', '$price', '$beds', 
            '$baths', '$commercial_type', '$area_sf', '$address', 
            '$description', '".$filename."',  now() )" ;

所以我需要在一行中插入我的所有$filename值。

但是当我运行此脚本时,只有最后一个$filename被插入到DB中。

如何在一行中将来自for循环的$filename的所有值插入一行。

1 个答案:

答案 0 :(得分:0)

你需要使用持久数组。

$fileNames = array();

for ($i = 0; $i < count($_FILES["user_files"]["name"]); $i++)
{
// get the image mime type
$image_mime = strtolower(image_type_to_mime_type(exif_imagetype($_FILES["user_files"]["tmp_name"][$i])));

if (in_array($image_mime, $valid_image_check))
{
    $folderName = "uploads/";
    $ext = explode("/", strtolower($image_mime));
    $ext = strtolower(end($ext));
    $filename = rand(10000, 990000) . '_' . time() . '.' . $ext;

    // if user upload a file abc,jpg, it will convert it to 291905_1399918178.jpg based on random number and time.
    $filepath = $folderName . $filename;
    if (!move_uploaded_file($_FILES["user_files"]["tmp_name"][$i], $filepath))
    {
        echo "fail uplaod";
    } else {
        $smsg .= "<strong>" . $_FILES["user_files"]["name"][$i] . "</strong> uploaded successfully. <br>";

        $magicianObj = new imageLib($filepath);
        $magicianObj->resizeImage(100, 100);
        $magicianObj->saveImage($folderName . 'thumb/' . $filename, 100);
        $fileNames[] = $filepath;
    }

} else {
    echo "not image";
}
}

然后在你的SQL中:

$images = rtrim(implode(',', $fileNames), ',');
$sql =  "INSERT INTO properties 
       (agent_id, property_name, category, location, 
        property_type, search_radius, price, bed_rooms, 
        bath_rooms, commercial_type, area, address, 
        description, image_name, date_added)
     VALUES 
       ('$agent_id', '$property_name', '$listing_for', '$city', 
        '$property_type', '$area', '$price', '$beds', 
        '$baths', '$commercial_type', '$area_sf', '$address', 
        '$description', '$images',  now() )" ;

这会将逗号分隔的文件名列表插入数据库。

您可能希望查看查询外观中的预准备语句。

您可能应该使用另一个表格来附加属性ID。

image_id | image_url | property_id
SELECT `image_url` FROM `property_images` WHERE `property_id` = :property_id

这些方面的东西,正如已经评论过的那样,这是一种糟糕的做事方式。