我需要在以下格式的數據庫中插入多張圖片, 09823754257b41f19e24c01471.jpg, 754257b41f19e24c01471422234.jpg,
插入在MySQL數據庫中的多個圖像使用每個循環
我寫了下面的代碼在數據庫中插入值
<?php include "dbconnect.php";
if(isset($_POST['property_add'])){
$propTitle = $_POST['title'];
$propSize = $_POST['size'];
$propType = $_POST['type'];
$propLoc = $_POST['location'];
$propStatus = $_POST['status'];
$propPrice = $_POST['price'];
$propDesc = $_POST['propDesc'];
if(isset($_FILES['files'])){
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){
$temp = explode(".", $_FILES['files']['name'][$key]);
$file_name = rand().uniqid().round(microtime(true)) . '.' . end($temp);
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
$desired_dir="../property/".$propTitle."/".$propStatus."/";
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0777, true); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
}else{ //rename the file if another one exist
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
if($tmep){
$photos[] = $file_name;
}
}
$files =implode(',', $photos);
$query = "INSERT INTO `property`(`property_name`, `property_size`, `property_type`,`property_loc`, `property_status`, `property_price`, `property_desc`, `property_photos`) VALUES ('$propTitle','$propSize','$propType','$propLoc','$propStatus','$propPrice','$propDesc','$files')";
mysqli_query($conn,$query);
echo "<script>alert('Property Added Successfully')</script>";
header('Location:../dashboard.php?tab=add&mode=success');
}
}
這裏的問題是,如果我選擇的兩個圖像img1
和img2
但上面的代碼生成用於文件的以下值
09823754257b41f19e24c01471.jpg, 754257b41f19e24c01471422234.jpg,8237542e24c0171422252. ,
我只希望在數據庫中插入兩個值,所以產生的第三個值,我不知道。
不存儲該信息爲csv。閱讀關於一對一關係 – Jens
是否有任何其他方式在此代碼中工作 – Prashanth
@Jens上面的意思是「一對多」的關係,對於那些你想要搜索它的人。 – halfer