[英]How to upload Multiple Images to a folder and store their path into one database column
我想將特定列表的多個圖像上傳到我的 PROPERTIES 表的 PROPERTYIMAGES 列/字段中。 我有一個工作代碼,它將圖像存儲在適當的 UPLOADS 文件夾中,但問題出在數據庫中。 它最終將 3 個列表插入到與為該列表選擇的三個圖像相對應的表中。
但是,我只想在數據庫中插入一行,然后對於“propertiesImages”,我希望將所有 3 個圖像路徑存儲到一列中。 我不知道我說的是不是真的,我只是希望有人能理解。
我將刪除代碼和可能的圖像快照
///插入記錄的PHP代碼//
<?php
session_start();
include "../incs/database/dbconfig.php"; //include the DB config file
if (isset($_POST['propertyTitle']) && isset($_SESSION['userid']) && $_SESSION['userid'] == true) {
//Retrieve Form Data From AJAX Parsing
$title = mysqli_real_escape_string($dbconn, $_POST['propertyTitle']);
$desc = mysqli_real_escape_string($dbconn, $_POST['propertyDescription']);
$pType = mysqli_real_escape_string($dbconn, $_POST['propertyType']);
$pStatus = mysqli_real_escape_string($dbconn, $_POST['propertyStatus']);
$pLocation = mysqli_real_escape_string($dbconn, $_POST['propertyLocation']);
$pMainLocation = mysqli_real_escape_string($dbconn, $_POST['mainLocation']);
$bedrooms = mysqli_real_escape_string($dbconn, $_POST['bedroomNumber']);
$bathrooms = mysqli_real_escape_string($dbconn, $_POST['bathroomNumber']);
$garage = mysqli_real_escape_string($dbconn, $_POST['garageNumber']);
$pNumber = mysqli_real_escape_string($dbconn, $_POST['propertyNumber']);
$pPrice = mysqli_real_escape_string($dbconn, $_POST['propertyPrice']);
$pAreaSize = mysqli_real_escape_string($dbconn, $_POST['propertyAreaSize']);
$pAreaPFT = mysqli_real_escape_string($dbconn, $_POST['areaPostfixText']);
$pVideo = mysqli_escape_string($dbconn, $_POST['propertyVideoURL']);
$features = "";
foreach($_POST['propertyFeatures'] as $feature) {
// Here $results holding all the check box values as a string
$features .= $feature. ",";
}
$propertyAuthor = mysqli_real_escape_string($dbconn, $_SESSION['userid']);
for($i = 0; $i < count($_FILES['propertyImages']['name']); $i++) {
$imageTempDirectory = $_FILES["propertyImages"]["tmp_name"][$i];
$imageName = $_FILES["propertyImages"]["name"][$i];
$filetype = $_FILES["propertyImages"]["type"][$i];
$pathForImageUpload = "uploads/".$imageName;
move_uploaded_file($imageTempDirectory,$pathForImageUpload);
//Submit Properties Data in Propertires Table
$propertyKwary = mysqli_query($dbconn, "INSERT INTO properties (propertyTitle,propertyDescription,pTid,pSid,pLid,mainLocation,bedroomNumber,bathroomNumber,garageNumber,propertyNumber,propertyPrice,propertyAreaSize,propertyAreaSizePostfix,propertyVideoUrl,propertyFeatures,uid,propertyImages,submittedDate) VALUES ('$title','$desc','$pType','$pStatus','$pLocation','$pMainLocation','$bedrooms','$bathrooms','$garage','$pNumber','$pPrice','$pAreaSize','$pAreaPFT','$pVideo','$features','$propertyAuthor','$pathForImageUpload',NOW())");
if ($propertyKwary) {
// echo'<script>alert("Property Submission Failed-"'. mysqli_error($dbconn). ')</script>';
echo 'Property Submitted Successfully';
// header("Location: submit-listing.php");
} else {
echo 'Property Submission Failed';
}
}
}
// HTML 表格輸入//
<input type="file" name="propertyImages[]" id="propertyImages" multiple accept=".jpg, .png, .jpeg" />
<br><br>
<div id="propertyImagesPreview"></div>
你應該重新考慮你的數據庫結構,避免做你想做的事情。 這就是為什么...
當前設置
PROPERTIES table
propertiesid integer, not null, primary key, auto-increment
propertyTitle varchar, not null
PROPERTYIMAGES varchar
[other fields...]
看起來你打算這樣做:
1 | "Title for image 1" | "image1-file1 image1-file2 image1-file3"
2 | "Title for image 2" | "image2-file1 image2-file2 image2-file3"
PROPERTYIMAGES 值用空格分隔, ;
, 或者是其他東西...
問題:
你應該做的是有3張桌子
PROPERTIES table
propertiesid integer, not null, primary key, auto-increment
propertiesTitle varchar, not null
[other fields...]
IMAGES table
imagesid integer, not null, primary key, auto-increment
filename varchar, not null
PROPERTIES_has_IMAGES
propertiesid integer, foreign key to PROPERTIES.propertiesid
imagesid integer, foreing key to IMAGES.imagesid
此設置允許您:
其他參考: 在 MySQL 字段中存儲 csv – 壞主意?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.