【问题标题】:How to insert multiple image names in a same row and display it php-mysql如何在同一行中插入多个图像名称并显示它 php-mysql
【发布时间】:2015-06-25 11:34:44
【问题描述】:

我在 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() )"  ;

此查询将我的值插入数据库。但这里 image_name ($filename) 包含 3 张图片。我通过使用数组获取这些名称并插入数据库。这里所有的字段都是单一的。但 image_name 包含 3 个值。当我在 for 循环中使用这个脚本时,总共插入了 3 行。当我使用外部 for 循环时,它将最后插入$filename。所以我的需要是我想将所有 3 个image_names 和其他数据添加到一行中。之后我需要获取所有数据显示它。我怎样才能做到这一点。帮帮我的小伙伴。谢谢。

【问题讨论】:

  • 如果要存储多张图片,那么显然是一对多的关系。您需要重新设计 MySQL 架构。

标签: php mysql insert


【解决方案1】:

使用此代码,

foreach ($filename as $key => $value) {
        $array[] = $value; 
    }
    $arrayvalue = json_encode($array);

$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', '".$arrayvalue."',  now() )"  ;

【讨论】:

  • 感谢您的回复。它显示Invalid argument supplied for foreach() 这个错误。并将空值插入到表行中。
  • 如果$filename是一个数组,可以直接使用json_encode。像这样 $filename = array("a","b","c"); $arrayvalue = json_encode($filename);
  • $images_array = 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)); $images_array[] = $filename = rand(10000, 990000) . '_' . time() . '.' . $ext; 我就是这样使用的。但是当我插入 $images_array 时,插入了“数组”名称。
  • json_encode($images_array);这样使用json在数据库中插入数组的值
  • 谢谢我创建了另一个表。参考property_id。它会产生很好的效果。感谢您的回复。
【解决方案2】:

你可以使用类似的东西:

 implode("|",$filename)

在 SQL 查询中像

$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', '". implode("|",$filename) ."',  now() )";

同样使用

explode("|",$filename)

当您检索数据时

【讨论】:

    【解决方案3】:

    使用下面的代码插入图片。

    $images_array   =   array("img1.jpg","img2.jpg","img3.jpg");
    
    $filename   =   implode( ",", $images_array );
    

    在 SQL 查询中像

    $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() )"  ;
    

    在检索数据时使用它来获取图像数组

    $images_array   = explode( ",", $data['image_name'] );
    

    【讨论】:

    • 谢谢,但我没有使用 img1.jpg、img2.jpg 那样我在 for 循环中使用 $filename = rand(10000, 990000) . '_' . time() . '.' . $ext;。所以它随机生成图像名称。那么我该如何使用这个帮助我..
    • 您不能创建 $filename 并插入相同的 for 循环。首先用一个循环上传所有图像并获取图像名称数组,然后使用插入查询。
    • 在 for 循环之前声明 $images_array = array(); .. 在循环内添加图像为 $images_array[] = $filename = rand(10000, 990000) . '_' . time() . '.' . $ext; .. 循环完成后,您将拥有图像数组。
    • 谢谢。我明白你所说的。我按照你说的做了,但是插入数据库的值就像'数组'。
    • $images_array[] =rand(10000, 990000) . '_' . time() . '.' . $ext; 在 for 循环和循环后使用这个 $filename = implode( ",", $images_array ); .... 使用您的查询。
    猜你喜欢
    • 1970-01-01
    • 2020-03-28
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 2022-01-17
    • 1970-01-01
    • 2011-08-10
    相关资源
    最近更新 更多