0

I have an image gallery where users will be uploading images from their phones into a database which then gets called onto my gallery page. Each entry has an ID, an image, and a description in the DB.

This is the code that exports the gallery:

    //include database connection
    include 'mysqlconnect.php';

    $sql="select * from images order by image desc";
    $query=mysql_query($sql);
    while($row=mysql_fetch_array($query))
    {
        $image=$row ['image'];
        $description=$row ['description'];    

        //call all images from DB
        echo '<a href="'.$image.'" title="'.$description.'" style="background-image:url('.$image.');"></a>'; 
    } 

The problem is when a user uploads photos with an iPhone. Because iPhones always save their images as "image.jpg", every time one is uploaded it overwrites the previous iPhone images in the gallery. I think this is because the gallery is only calling for file names.

Is there a way to call for the images based on their row ID? Something like $image=$row['id','image']; ?

Paul Ruocco
  • 462
  • 3
  • 18

1 Answers1

2

Firstly, if the same image name gets uploaded and you haven't made provisions to rename it or used a distinctive folder to use them as being unique, then that is a problem right there.

If you want to view a photo based on an ID, then use it in your query.

I.e.:

$sql="select * from images WHERE id='some_id' order by image desc";

"Is there a way to call for the images based on their row ID? Something like $image=$row['id','image']; ?"

No, that is invalid syntax:

$image=$row['id','image'];

You would need to use two seperate $row(s).

I.e.:

$id = $row['id'];
$image=$row['image'];

Sidenote: You may not even need to use the WHERE clause, and just use the $row['id'].

However, if you're set on displaying images based only on a particular user, then yes; use a WHERE clause and even using sessions based on their session username.

Rename uploaded files:

Consult the following on how to use a unique name on uploads:

You're also using a deprecated MySQL library. Best you move over to either mysqli_ or PDO, as it will be removed from future PHP releases.

References:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Thanks so much for the info. Databases are not my strong suit... So, in my database I have an auto incrementing ID. Just to verify, you're saying it is not possible to say "show image.jpg from ID #1, show image.jpg from ID #2, etc." without renaming the uploaded files or placing them into folders? – Paul Ruocco Oct 10 '15 at 18:30
  • 1
    @PaulRuocco You're welcome. No, because you'll only show the same image, unless that's what you want to do. If not, then your uploaded file will need to be renamed with a unique name and saved to the database using that name and according to the user/id who uploaded it. – Funk Forty Niner Oct 10 '15 at 21:06