Jump to content

need help with sql upload


newb

Recommended Posts

i want the file name to be renamed to the same as the id number thats auto_generated in the database but its not!.

[code]
<?php                                 
                                  if (move_uploaded_file($file_tmp,$upload_dir.$file_name)) {
                                  // success!
include "dbconfig.php";
    $query = mysql_query("SELECT * FROM `style_imgupload` ORDER BY id ASC LIMIT 2");
    $row = mysql_fetch_array($query);
   
    $uid = $row['id'];
    rename("styles/$file_name", "styles/$uid$ext");


                                      echo "File $i: ($uid$ext) Uploaded.<br>";
 
                                  }else{
                                        echo "File $i: Faild to upload.<br>";
                                  }#end of (move_uploaded_file).

                            }#end of (file_exists).

                      }#end of (file_size).

                }#end of (limitedext).

          }#end of (!is_uploaded_file).

      }#end of (for loop).
     
$id = mysql_insert_id();
$insert = mysql_query("INSERT INTO style_imgupload (id, active, author, author_url, ip_address) VALUES ('$id', '$active', '$author', '$author_url', '$ip_address')");
if (!$insert) {
    die('Invalid query: ' . mysql_error());
}
?>
[/code]
whenever i submit information it doesnt rename to the same id as whats generated in the database. please help!
Link to comment
Share on other sites

ok i fixed the mysql database problem, but whenever i submit something it doesnt rename to the same id as whats in the database. i want the file name to be renamed to the same as the id number thats auto_generated in the database but its not!. please help!
Link to comment
Share on other sites

well, its suppose to rename the file to the next number. kinda like auto_incrementing on tables in sql.

here's my sql table:
[img]http://www.flickcabin.com/sessions/306f2f2b636bb59e1cf9d7d43b71c978a1eed72c/table.PNG[/img]

whenever i upload a file, its suppose to rename that file to the exact id number as whats in the id field on that table, but its not doing that :(.

however, whenever i upload something or a file, it inserts the data into the table just fine, but it doesnt rename the file to the same id number as in the table, it just renames it to something else.
Link to comment
Share on other sites

it just renames them to 1.jpg, im assuming it does that because thats the id number in the first row. im fully aware my code is this:
[code=php:0]$uid = $row['id'];
rename("styles/$file_name", "styles/$uid$ext");
[/code]
im guessing thats why it does that. however, i dont want it to rename a file to 1.jpg each time i upload something, and thats what its doing. is there a way to fix it??? any one can help??
Link to comment
Share on other sites

Use this the first time when you select data, order it by id descending and limit it to 1.
when renaming set the file name to the resulted id +1.

That means
Change

$query = mysql_query("SELECT * FROM `style_imgupload` ORDER BY id ASC LIMIT 2")

to

$query = mysql_query("SELECT * FROM `style_imgupload` ORDER BY id DESC LIMIT 0, 1")

and

rename("styles/$file_name", "styles/$uid$ext");

to

$uida = $uid+1;
rename("styles/$file_name", "styles/$uida$ext");
Link to comment
Share on other sites

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.