Jump to content

Archived

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

newb

need help with sql upload

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!

Share this post


Link to post
Share on other sites
Try "SELECT MAX(ID) as ID ..." and increment it by one (id++;) before re-uploading. I haven't looked close enough to figure out why you are getting two rows, but this should be a start.

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites
its ok, can you help me please?? i am prety newbie here =[

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
eh i just told you in the post above. its suppose to rename the file to the exact id number as whats being generated in the id field on that table whenever i hit the submit button to upload something.

Share this post


Link to post
Share on other sites
I am aware of what it is suposed to do, I would like to know what it is actually doing though. So instead of renaming it to the id like it is suposed to, what is it actually renaming it to? You say "something else", what exactly is that something else?

Share this post


Link to post
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??

Share this post


Link to post
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");

Share this post


Link to post
Share on other sites
works! thanks a bunch dude!!1 i was starting to give up on here =[

Share this post


Link to post
Share on other sites

×

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.