我想使用PHP MYSQL上传同一行中的多个图像名称
这是我的密码我想使用PHP MYSQL上传同一行中的多个图像名称,php,mysql,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, $va
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";
}
}
因此,这里我需要在一行中插入我的所有$filename
值
但是当我运行这个脚本时,只有最后一个$filename
被插入到数据库中
如何将for循环中的
$filename
的所有值插入到一行中的DB中。对此,您需要使用一个持久数组
$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";
}
}
这将在数据库中插入以逗号分隔的文件名列表
您可能希望从查询的外观中查看准备好的语句
您可能应该为类似的内容使用另一个表,它附加到属性ID
image_id | image_url | property_id
SELECT `image_url` FROM `property_images` WHERE `property_id` = :property_id
这是一种糟糕的做法。你应该创建一个新表,让我们称之为
property\u images
,并在其中为你收到的每个图像存储一行,其中property\u id
作为链接字段。正如@riggsfully所说,如果你根据一条记录存储多个图像,然后您应该创建另一个表来保存这些图像。好的,谢谢。如何编写代码。你能回答我吗?我不太清楚为什么我要为你做所有的工作。拿一本书,自己动手。带着具体的有限问题回来,如果你有任何具体的问题,问另一个问题problems@RiggsFolly我没有问你,伙计。这是一个解决方案,但很糟糕。我已经按照他希望的方式提供了一个快速的解决方案。你能解释一下如何做得更好吗?谢谢你的回答。我正在使用你的代码。但插入的所有值都表明列image_name为空。那么我现在能做什么呢?是的,但解决方案是在阅读了一些关于数据库设计的书籍后,将其撕碎并重新开始,他甚至不太可能费心去阅读它。您现在还需要担心编码一些东西,这些东西将允许代理为该属性添加或删除图像。现在,每次需要进行任何更改时,都必须处理一个逗号分隔的文件名列表。凌乱、代码密集且容易出错重新设计数据库
$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() )" ;
image_id | image_url | property_id
SELECT `image_url` FROM `property_images` WHERE `property_id` = :property_id