views:

11

answers:

1

The page has to INSERT a row and MAKE a directory with the name of the ID of that row when the user clicks the upload button, will this script work?, what could possibly go wrong?.

(when the upload button has been clicked)
//insert the new row
$query="INSERT INTO photoalbum (userid) VALUES ($userid)";
mysql_query($query);

//get the id of that row to make a directory with that name
$getid="SELECT id FROM photoalbum WHERE userid = $userid order by id desc limit 1";
$result=mysql_query($getid);
$thename=mysql_results($result,0,"id");
mkdir('userimages/$userid/photoalbums/$thename');

{ script that upload the files in the new folder }

Is it safe to INSERT and SELECT the row I just inserted in the same page, just right after an insert has been done? I was thinking what if the SELECT query don't pick the row i just inserted but instead the previous row, because the INSERT isnt fast enough and the SELECT query gets the previous row instead. Is this a possible scenario?

A: 

Why not use mysql_insert_id() ?

As for your question if the insert is successful, then there is no problem with doing a select even a millisecond after the insert.

I just noticed another problem:

mkdir('userimages/$userid/photoalbums/$thename');

Due to single quotes the $userid and $thename variables will be used literally, so instead use double quotes like this:

mkdir("userimages/$userid/photoalbums/$thename");
Sabeen Malik
i had no idea that mysql_insert_id existed, im fairly new to php. thanks
FlavourFlave
Yup its a pretty handy thing to have, it would erase the need of your select query altogether.
Sabeen Malik
I added more info to my answer, hopefully it will resolve your problem.
Sabeen Malik