dystopia
dystopia

Reputation: 93

Associating an Uploaded Image File with Existing Album

I am relatively new to PHP/MySQL and have been having a problem that I cannot figure out. I've scoured this site but cannot find any information.

My problem is very simple: How to associate an uploaded image file with a pre-existing album.

I have 2 separate tables in my database: one for albums and one for images. What I want to do is have an <option> drop down menu retrieving the user's pre-existing albums thus assigning an albumID to the image database.

I have successfully populated a drop down box with the user's albums, but cannot figure out how to insert the selected ID into the picture table albumid.

Any help would be much appreciated. Thank you!

Upvotes: 1

Views: 299

Answers (3)

Steven
Steven

Reputation: 19445

When you selectd album from drop down, you could reload the page, adding the album ID in the URL. Then you only need to use $_GET to get the album id.

Then, when you upload the image, in your upload.php file, you use the $_GET to retrieve the album ID.

Once you have successfully moved the image from the upload temp directory to it's final location, add the image URL to the image table and retrieve last inserted row ID. Take this ID and insert it into album table.

Does that answer your question? :)

Upvotes: 1

Peter Lange
Peter Lange

Reputation: 2886

Ok, Assuming that you have the file Upload working correctly and have no problem inserting the relevant picture data into the picture table:

<form method="post" enc-type="multipart/form-data">
    <select name="Albums">
        <option value="1">My First Album</option>
        <option value="2">My Secret Album of Dirty Pictures</option>
    </select>

    <input type="file" name="MyFile" />
</form>

in your code you would get the id of the selected album from the POST variables in the following manner, similar to how you accessed the file upload information. Then you input it all into your mysql table at the same time.

<?php 

     $AlbumId = $_POST["Albums"];
     $Picture = $_FILES["MyFile"]["name"];

     $Sql = "INSERT INTO Pictures (AlbumId, Picture) VALUES ($AlbumId, '$Picture')";
     mysql_query($Sql);
?>

Upvotes: 0

Sparkup
Sparkup

Reputation: 3754

Say you have album1, in picture table you would use the following :

UPDATE picturetable SET albumid = 'album1' WHERE ID = 'the_picture_id'

Upvotes: 0

Related Questions