0

i am trying to store image in folder and 2nd part is store image name in database.
this works fine but the problem is that no matter how many images u upload, there is only one image in folder name 0.jpg.

 $sql = mysql_query("SELECT * FROM item WHERE image_user_name = '$image_user_name_p'");
$existCount = mysql_num_rows($sql);
if($existCount >= 1)
    {
        $add_item_error .= "That image name is already taken";
         exit();
    }

   //place image in folder
        $pid = mysql_insert_id(); 
        $image_folder_name = "$pid.jpg";
        move_uploaded_file($_FILES['image_file']['tmp_name'], "IMAGE/ITEMS/$image_folder_name"); 

    //add this product into the database now
    $sql = mysql_query("INSERT INTO item (image_folder_name)
    VALUES('$image_folder_name'");

i am trying to set $pid be different every time. ex 0,1,2,3,4,5....
and iam getting the id of image from database by mysql_insert_id();
so if in database image last image id is 3. than $pid = 3. this way i will get different value for pid

5
Contributors
7
Replies
8
Views
4 Years
Discussion Span
Last Post by diafol
4

This function mysql_insert_id()

Read this:

http://php.net/manual/en/function.mysql-insert-id.php

Try another alternative.

You forgot to put another parentheses here:

From this:

$sql = mysql_query("INSERT INTO item (image_folder_name) VALUES('$image_folder_name'");

to this:

$sql = mysql_query("INSERT INTO item (image_folder_name) VALUES('$image_folder_name')");
Votes + Comments
To Rectify what some retard did to LastMitch
1

I would suggest that you create a random filename for your image and keep an attribute "id" in your database table with auto-increment feature. In this way you can get what you want. Use rand(x, y) method.

Or else, according to your procedure, after using mysql_insert_id() - you are getting the last inserted row's id. Right?
So, you can use this value + 1 to set a new id.

1

Alternatively, use a timestamp for the filename:

$filename = time() . '_' . $user_id . '.' . $ext;

That should make a unique filename without chance of collision.

Edited by diafol

1

that seem to be a good plan. just one question what is ext?

$ext is referring to the file extension (.jpeg, .png, etc.).

0

i tried using: exif function to get the type
$ext = exif_imagetype($_FILES['image_file']['tmp_name']);
but this return '2'.

1

No use pathinfo:

$ext = pathinfo($_FILES['image_file']['name'], PATHINFO_EXTENSION);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.