簡體   English   中英

如何將多個圖像上傳到一個文件夾並將它們的路徑存儲到一個數據庫列中

[英]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 值用空格分隔, ; , 或者是其他東西...

問題:

  • 文件名可以包含空格。
  • 您無法構建有效的查詢,因為您必須拆分 PROPERTYIMAGES 以查看特定文件是否在字段中。
  • 假設您要刪除特定文件,您將不得不再次循環進入整個表以刪除條目。
  • 此配置不是 NF(標准形式)。

你應該做的是有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

此設置允許您:

  • 輕松刪除圖像。 只需將其從 IMAGES 表中刪除即可。 如果您正確設置了 ON DELETE 規則,PROPERTIES_has_IMAGES 中的所有條目也將被自動刪除(參照完整性)。
  • 輕松查詢哪個文件是哪個屬性。
  • 您可以輕松地強制執行唯一圖像,因此不會使用 2 個相同的圖像。
  • 由於鍵被索引,因此速度要快得多。
  • 在嘗試擴展簡單的數據庫結構時將避免將來出現問題。 例如,您將能夠將圖像鏈接到其他表格。

其他參考: 在 MySQL 字段中存儲 csv – 壞主意?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM