Jump to content

My Script has flaws, could someone help me out? Calculating and ID


Recommended Posts

Hey All,

 

Okay I have a photo uploader script that basically uploads a photo to the server and renames it.

The name is processed by somethings:

//Calculate next ID number.
$query = 'SELECT * from photos order by photo_id desc limit 1';
$calcid = mysql_fetch_array(mysql_query($query));

        $id = $calcid['photo_id'] + 1;

 

Right Im expecting the photo uploader to be used a lot at any one time so I' if I used this kind of script bad things will start to happen? I need some help in calculating a name, I thought about using random ID? If I use Random ID's at any point could one be the same? Is there a better way I could do this (well yeah obviously).

 

Many thanks

 

Jordan

If you want the actual file name to use an unique ID from a database table, use an auto_increment field in the table. INSERT the row in the table and use mysql_insert_id() to get the ID that was just assigned. If the whole filename consists of more than just the ID, insert empty data for the actual filename and go back and execute an UPDATE query to fill in the actual information once it has been assigned.

Well basically what goes on is,

 

You upload a file, select the file -> upload -> script uploads it to the server after it assigns the file you uploaded a new name which should be unique.

Once that's processed it inserts to a database the ID (which also doubles as the file name, (it doesnt have to though) and then the file is viewed on the site.

 

Jordan

I haven't actually been able to try test out the theory properly but thinking about it, for the way in which you describe it you'd need to run the insert query first then process the file then go back and update the table? If this is the case then if something goes wrong half way through executing the script then I'd be left with unnecessary rows from error'd uploads? Well at least there could be a chance right?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.