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
https://forums.phpfreaks.com/topic/17286-need-help-with-sql-upload/
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.
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??
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");

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.