0

I create upload image and move image to upload folder. When someone uploads image that mysql_insert_id() will insert unique id (1, 2, 3, 4, 5 so on....) and image extension (jpg, gif, png), I need the result looks like this (1.jpg, 2.gif, 3.png, 4.jpg, 5.gif, 6.png so on.......)

1
  • Could you make the question more clear? It's unclear what your problem is exactly. Commented Dec 4, 2012 at 1:43

1 Answer 1

1

I think the issue is in this bit of code:

mysql_query("INSERT INTO users VALUES('', '$image_file')") or die (mysql_error());
$image_id = mysql_insert_id();
$image_file = $image_id.'.'.$extension;

Looks as though you're using $image_file in the sql before it's been created.. Try this instead:

$image_file = $image_id.'.'.$extension;
mysql_query("INSERT INTO users VALUES('', '$image_file')") or die (mysql_error());
$image_id = mysql_insert_id();

Erm no

After another look at your code I can't see where you're setting $image_file for use in the sql at all, I noticed you were using the returned mysql_insert_id after posting my answer, sorry for the confusion there.

I think you need to generate the $image_file before using it in the sql, and again after as you look to be wanting to use the returned mysql_insert_id.

$image_file = $_FILES['image']['name'];
mysql_query("INSERT INTO users VALUES('', '$image_file')") or die (mysql_error());
$image_id = mysql_insert_id();

Also

As you are renaming the file you also need to update the stored image name in the database after creating the record:

$image_file = $_FILES['image']['name'];
mysql_query("INSERT INTO users VALUES('', '$image_file')") or die (mysql_error());
$image_id = mysql_insert_id();
$image_file = $image_id . '.' . $extension;
move_uploaded_file($_FILES["image"]["tmp_name"], "upload/".$image_file);
mysql_query("UPDATE users SET image_column = '{$image_file}' WHERE id_column = {$image_id}") or die(mysql_error());

Because you haven't used the columns names in your sql statements I've used generic terms you will need to insert your own columns instead.

Sign up to request clarification or add additional context in comments.

4 Comments

then what is value of $image_id above insert query ?
@GBD Yes I was a little confused there!
Now you are right but then after successful upload of file.. he needs to update image file name into db
Sorry about that, I just know to write simple query. $image_id is mysql_insert_id(). I already switched around you told me but .jpg inserted database and 0.jpg moved to upload folder. How to solve it? I need to have image extension jpg, gif, png insert database and move to upload folder has unique id and image extension looks like this (1.jpg, 2.gif, 3.png, 4.jpg, 5.gif, 6.png so on.......) Please help, thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.