-1

I want to upload Multiple Images for a particular listing into the PROPERTYIMAGES Column/Field of my PROPERTIES Table. I've a working code though,it stores the images in the appropriate UPLOADS folder, But the problem is in the Database. It ends up inserting 3 listings into the Table which corresponds with the THREE IMAGES selected for that LISTING with .

However i want to insert just one Row in the database then for the 'propertiesImages', I want all 3 images path to be stored into one column. I don't know if i'm saying this rigth, I just wish someone will understand.

I'll drop the codes and possibly Image Snapshots

///PHP CODE TO INSERT RECORDS//

<?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 FORM INPUT//

<input type="file" name="propertyImages[]" id="propertyImages" multiple accept=".jpg, .png, .jpeg" />
                    <br><br>
                    <div id="propertyImagesPreview"></div>
ayamjames
  • 11
  • 4
  • @Dharman Thanks for ya suggestion. But i'm just working on a project, am still Developing $ Testing out the System. I have plans for Security already in place. Thanks anyways – ayamjames Oct 11 '19 at 23:00
  • @Dharman Thanks for ya suggestion. But i'm just working on a project, am still Developing $ Testing out the System. I have plans for Security already in place. Thanks anyways – ayamjames Oct 11 '19 at 23:01
  • 1
    Why do you even bother with `mysqli_escape_string`, which you will have to remove sooner or later and use prepared statements instead? It would be easier for you to start straight away with prepared statements. – Dharman Oct 11 '19 at 23:01

1 Answers1

2

You should rethink your database structure and avoid doing what you are trying to do. Here is why...


Current setup

PROPERTIES table
    propertiesid       integer, not null, primary key, auto-increment
    propertyTitle      varchar, not null
    PROPERTYIMAGES     varchar
    [other fields...]

Looks like you are planning to do:

1  |  "Title for image 1"  |  "image1-file1 image1-file2 image1-file3"
2  |  "Title for image 2"  |  "image2-file1 image2-file2 image2-file3"

The PROPERTYIMAGES values are to be separated by spaces, ;, or something else...

Problems:

  • filenames can contain spaces.
  • you cannot build efficient queries since you have to split the PROPERTYIMAGES to see if a particular file is in a field.
  • lets say you want to delete a particular file, again you will have to loop into the entire table to remove the entries.
  • this configuration is not NF (normal forms).

What you should do is have 3 tables

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

This setup allows you to:

  • remove an image easily. Just remove it from the IMAGES table. If you have setup your ON DELETE rule properly, all entries in PROPERTIES_has_IMAGES will automatically be deleted as well (referential integrity).
  • easily query which file is which property.
  • you can easily enforce unique images, so no 2 identical images are used.
  • much faster since keys are indexed.
  • will avoid future issues when trying to expand on the simple database structure . You will be able to link images to other tables for example.

Other reference: Storing csv in MySQL field – bad idea?

Nic3500
  • 8,144
  • 10
  • 29
  • 40