• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to display only the images loaded by a specific user?

Explorer ,
Aug 31, 2015 Aug 31, 2015

Copy link to clipboard

Copied

I did get my program to work, I able to upload a image save as a file in the database and display it. There's just one thing, when I go to select a image that has been loaded by a user it shows me every image that was loaded, how do I just only show the images loaded by the specific user signed in to select ?

Here's the code for the loading the file:

<?php require 'Connections/Connections.php'; ?>

<?php

  if(isset($_FILES['UploadFileField'])) {

  $UploadName = $_FILES['UploadFileField']['name'];

  $UploadName = mt_rand(100000, 999999).$UploadName;

  $UploadTmp =  $_FILES['UploadFileField']['tmp_name'];

  $UploadType = $_FILES['UploadFileField']['type'];

  $FileSize = $_FILES['UploadFileField']['size'];

  $UploadName = preg_replace("#[^a-z0-9.]#i", "", $UploadName);

  

  if(($FileSize > 1000000)) {

  die("Error - File Size");

  }

  if(!$UploadTmp){

  die("No File Seleced, Try Uploading again");

  }else{

  move_uploaded_file($UploadTmp, "Upload/$UploadName");

  }

  }

  ?>

<!doctype html>

<html>

<head>

<link href="CSS/Master.css" rel="stylesheet" type="text/css" />

<link href="CSS/Menu.css" rel="stylesheet" type="text/css" />

<meta charset="utf-8"

<title>File Upload</title>

</head>

<body>

<div class="Container">

  <div class="Header"></div>

  <div class="Menu">

    <div id="Menu">

      <nav>

        <ul class="cssmenu">

         <li><a href="Account.php">Account</a></li>

        

                    <li><a href="UpdateAccount.php">Update Account</a></li>

                   

                    <li><a href="NewRequest.php">New Request</a></li>

                   

                    <li><a href="ReviewRequest.php">Review Request</a></li>

                   

                    <li><a href="LogOut.php">Log Out</a></li>

        </ul>

      </nav>

    </div>

  </div>

  <div class="LeftBody"></div>

  <div class="RightBody">

  <p class="p"> Please include all files you would like to be  uploaded below.</p>

    <form action="FileUpload.php" method="post" enctype="multipart/form-data" name="FileUploadForm" id="FileUploadForm">

      <label for="UploadFileField"></label>

      <input type="file" name="UploadFileField" id="UploadFileField">

      <input type="submit" name="UploadButton" id="UploadButton" value="Upload">

       <p class="p"> When Finish Return to New Request to Include the Files</p>

    </form>

  </div>

  <div class="Footer"></div>

</div>

</body>

</html>

Here's the code for selecting and storing the image:

<?php require 'Connections/Connections.php'; ?>

<?php

  if(isset($_POST['NewRequest'])) {

  session_start();

  $FName = $_POST['First_Name'];

  $Location = $_POST['Location'];

  $Description = $_POST['Description'];

  $image_name = $_POST['image_name'];

if($image_name == "") {

$no_image_selected = true;

}

else {

$image_selected = true;

}

  $sql = $con->query("INSERT INTO newrequest (Fname, Location, Description, image_name)Values('{$FName}','{$Location}','{$Description}','{$image_name}')");

  header('Location: Account.php');

  }

?>

<!doctype html>

<!doctype html>

<html>

<head>

<link href="CSS/Master.css" rel="stylesheet" type="text/css" />

<link href="CSS/Menu.css" rel="stylesheet" type="text/css" />

<meta charset="utf-8"

<title>NewRequest</title>

</head>

<body>

<div class="Container">

  <div class="Header"></div>

    <div class="Menu">

    <div id="Menu">

        <nav>

                <ul class="cssmenu">

                  <li><a href="Account.php">Account</a></li>

        

                    <li><a href="UpdateAccount.php">Update Account</a></li>

                   

                    <li><a href="NewRequest.php">New Request</a></li>

                   

                    <li><a href="ReviewRequest.php">Review Request</a></li>

                   

                    <li><a href="LogOut.php">Log Out</a></li>

              </ul>

             </nav>

             </div>

    </div>

    <div class="LeftBody"></div>

    <div class="RightBody">

      <form "insert_info" action="" method="post" name="NewRequest" id="NewRequest">

      <div class="FormElement">

        <label for=""></label>

        <input name="First_Name" type="text" required class="TField" id="First_Name" placeholder="First Name" >

      </div>

      <div class="FormElement">

        <label for=""></label>

        <input name="Location" type="text" required class="TField" id="Location" placeholder="Location" >

      </div>

      <div class="FormElement">

        <label for="Description"></label>

        <textarea name="Description" required class="TField" id="Description" placeholder="Description" ></textarea>

      </div>

<div class="FormElement">   

</div>

<a class = afield href="FileUpload.php">File Upload</a>

      <select name="image_name" id="image_name">

<option value="">Select Image</option>

<?php

$a = array();

$dir = 'Upload';

if ($handle = opendir($dir)) {

while (false !== ($file = readdir($handle))) {

if (preg_match("/\.png$/", $file)) $a[] = $file;

elseif (preg_match("/\.jpg$/", $file)) $a[] = $file;

elseif (preg_match("/\.jpeg$/", $file)) $a[] = $file;

elseif (preg_match("/\.gif$/", $file)) $a[] = $file;

}

closedir($handle);

}

natsort($a);

foreach ($a as $i) {

echo "<option value=".$i.">".$i."</option>";

}

?>

</select>

<input name="NewRequest" type="submit" class="button" id="Send" value="Send">

</form>

<?php

if(isset($image_selected)) {

echo "<p>Image name successfully inserted into database</p>";

}

elseif(isset($no_image_selected)) {

echo "<p>Please select an image name</p>";

}

?>

    </div>

   

    <div class="Footer"></div>

</div>

</body>

</html>

CaptureFU.PNG

When I go to select image, I just want to be able to view the images uploaded by the users sign in, not by every user that has uploaded a image.Is there a way to do this any suggestions will be helpful?

Views

788

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 31, 2015 Aug 31, 2015

Copy link to clipboard

Copied

Your database table must contain a unique ID or key for each record.  Usually an int(11) that auto_increments. You must capture that ID in your SQL statement.

Nancy O.

Nancy O'Shea— Product User, Community Expert & Moderator

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Aug 31, 2015 Aug 31, 2015

Copy link to clipboard

Copied

If the code that displays the images uses something like scandir(), which displays all the files in the directory, then you should be putting the images in separate directories based on the user_id, like this:

images/25/photo1.jpg

images/25/anotherphoto.jpg

images/28/sunset.jpg

images/28/hawaii.jpg

. . .then you can programmatically select the appropriate directory based on the user.

By the way, you need to sanitize and validate your input from the user so that a malicious user doesn't upload malicious code to the server or database. This is true even if users must be logged in and are trusted,because their account may get hacked.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 31, 2015 Aug 31, 2015

Copy link to clipboard

Copied

Are you referring to creating more tables in my database for each user?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 31, 2015 Aug 31, 2015

Copy link to clipboard

Copied

You would ideally need to store the image names/numbers in a seperate table and associate the user_id with the image

So in your 'image' table you would have 3 columns:

image_id          user_id                     image_name

1                             10                              1234

2                              10                              8096

3                              5                                  6582

4                              7                                  8765

5                              7                                  6765

6                             10                                 9876

Above you see user_id 10 has uploaded 3 images, user_id 7 has uploaded 2 images and user_id_5 has uploaded 1 image

Then you would select the images from the 'images' table based on the user_id once they have signed in:

SELECT image_name FROM images WHERE user_id = 10

So when a user signs in to upload an image you would append the 'user_id' to a hidden form field :

<input type="hidden" name="user_id" value="<php echo $userInfo['user_id']; ?>" >

Then get the user_id and the image_name once the form is submitted to store in the information in the database table  'images'

$user_id = $_POST['user_id'];
$image_name= $_POST['image_name'];
INSERT INTO images (user_id, image_name) VALUES ('$user_id' , '$image_name')

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 31, 2015 Aug 31, 2015

Copy link to clipboard

Copied

What I have is a database that has different information for a new location request with image.

I tried to use my User Id but it would never stay the same. When I register, I would get a user id but when I went to put in a new request under the user it would change, sometimes it would stay  the same for the first and than change once I added in another one, but when I went to review it it would never show all of them for that user so I change it to use Fname field and than it should the same first name for every request and each review.

So I would have to use Fname, but are you saying I need to change the code I have for select or add in additional code?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 31, 2015 Aug 31, 2015

Copy link to clipboard

Copied

emrenilo32520834 wrote:

What I have is a database that has different information for a new location request with image.

I tried to use my User Id but it would never stay the same.

An id column in a database WILL always be the same IF you allocate it as the 'primary' key and use 'auto-increments'.

You virtually always need a 'unique' column in your database to identify records otherwise you have no stable way of getting the correct information back - using the Fname column is no good as many users many have the same Fname.

This should be quite simple.

A user signs in with their username and password to ulpoad an image - you also select their 'unique' id from the table when validating their username and password. That 'unique' id is then used to 'track' everything they upload and everything that you wish them to be able to view.

You should not try to use one table to store all your information UNLESS it is very simple. You are now getting to the point where your requirements are starting to become more complex and as such information should be spilt across tables BUT be 'associated' by the 'user_id'.

If you a have the user_id you can choose information from any number of tables as my previous example showed - the user_id is associated with each of the images uploaded by that user and so can easily be selected based on the used_id

Having said all that unless you have some knowledge of database construction and are able to manipulate some code the chances of you succeeding in doing what you require is zero. Using the Dreamweaver server behaviours dosent help either (if you are using them for your login/password page). I don't know if Robs way is any simpler maybe he can enligthen us.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 01, 2015 Sep 01, 2015

Copy link to clipboard

Copied

I have more than one table; the problem I was having with my id was, after I created another request from that user only one would show up not all. I'm going to go back and try to change it to id and see what happens. And try the a little bit of the code above. Do you know what will be the best way to set that up to avoid the problem for future reference?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 01, 2015 Sep 01, 2015

Copy link to clipboard

Copied

LATEST

Never mind about the user ID that was always my primary Key; I just change it in my account page so my user name would show not my user id, that's what I was thinking about, you where referring to my user id in my database.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 31, 2015 Aug 31, 2015

Copy link to clipboard

Copied

In case you don't know, there is a brand new forum dedicated to coding questions such as this:

Coding Corner

Nancy O'Shea— Product User, Community Expert & Moderator

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Sep 01, 2015 Sep 01, 2015

Copy link to clipboard

Copied

Moving this query to Coding Corner.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines