Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在同一行中插入多个图像名称并显示它_Php_Mysql_Insert - Fatal编程技术网

Php 如何在同一行中插入多个图像名称并显示它

Php 如何在同一行中插入多个图像名称并显示它,php,mysql,insert,Php,Mysql,Insert,我正在努力帮助我。 我正在使用 $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

我正在努力帮助我。 我正在使用

$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\u name$filename包含3个图像。我通过使用数组和插入DB来获得这个名称。这里所有的字段都是单字段。但是图像名称包含3个值。当我在for循环中使用这个脚本时,总共插入了3行。当我使用outside for循环时,它将插入最后一个$filename。因此,我需要将所有3个图像名称和其他数据添加到一行中。之后,我需要获取所有数据并显示它。我该怎么做呢。帮帮我,伙计们。谢谢。

使用此代码

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

使用下面的代码插入图像

$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() )"  ;
$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() )";
当检索数据时,使用此命令返回图像数组

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

您可以使用以下内容:

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

检索数据时

如果要存储多个图像,则显然是一对多关系。您需要重新设计MySQL架构。感谢您的回复。它显示为此错误提供的无效参数。如果$filename是一个数组,您可以直接使用json_编码。像这样$filename=arraya,b,c$arrayvalue=json_encode$filename$图像_数组=数组;对于$i=0$i