Uploading a certain amount of pictures

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Uploading a certain amount of pictures

Post by chris12295 »

I have a MySQL table of picture url's that users can upload and asscociate with an online ad. I need to only let them associate 4 picures and if they upload another replace the first then second and so on....

Any help doing this would be greatly appreciated.

Picture TABLE:

id
adID
userID
src

Ad table:

adID
userID
...
User avatar
musashi
Forum Commoner
Posts: 39
Joined: Tue Jul 23, 2002 12:51 pm
Location: Santa Cruz - CA

Steps to take

Post by musashi »

I responded to another question on image file uploads, so you can check that out if you would like information on how to let users upload a file and how to store that file as a file on the server or as source in a mysql db.

Assuming you already know that part... here are the logic steps I see in your program. (Starting from, file has been uploaded)

1. Check user information in db.
2. If user has 4 files in db, delete first file
3. Insert new file

Since mySQL maintains a FIFO ordering, you can eliminate the files in the order they are retrieved. So it might look something like this:

$result = mysql_query("
SELECT * FROM TABLE
WHERE user_id=$user_id"
,$dblink);
if(mysql_num_rows($result) >= 4) { //should never be greater than 4
$deleteMe = mysql_fetch_object($result);
mysql_query("
DELETE FROM TABLE
WHERE id = $deleteMe->id"
,$dblink);
}

//I assume "id" is an auto_incremented primary key, so each value should
//be unique.

mysql_query("
INSERT INTO TABLE (adID,userID,src)
VALUES ($adID,
$userID,
$src)
,$dblink);

If you want the "src" field to contain the actual picture data (you can see this in the other posting) remember to set it as a binary field type (blob, mediumblob, etc.)
Post Reply