Jump to content


Photo

need help with sql upload


  • Please log in to reply
17 replies to this topic

#1 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 11 August 2006 - 09:27 PM

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

<?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());
}
?>
whenever i submit information it doesnt rename to the same id as whats generated in the database. please help!

#2 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 11 August 2006 - 09:44 PM

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.
PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)

#3 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 12 August 2006 - 03:19 AM

didnt help

#4 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 12 August 2006 - 03:31 AM

someone pleasee help!111

#5 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 12 August 2006 - 03:59 AM

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!

#6 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 12 August 2006 - 04:09 AM

ur code didnt help.

#7 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 12 August 2006 - 04:16 AM

Sorry, I totally mis-understood your question. A bit late here.

#8 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 12 August 2006 - 04:17 AM

its ok, can you help me please?? i am prety newbie here =[

#9 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 12 August 2006 - 04:21 AM

What exactly does it rename the file name to?

#10 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 12 August 2006 - 04:26 AM

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

here's my sql table:
Posted Image

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.

#11 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 12 August 2006 - 04:33 AM

Yeah, I am curious what it does rename it to?

#12 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 12 August 2006 - 04:37 AM

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.

#13 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 12 August 2006 - 04:38 AM

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?

#14 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 12 August 2006 - 04:42 AM

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:
$uid = $row['id'];
rename("styles/$file_name", "styles/$uid$ext"); 
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??

#15 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 12 August 2006 - 05:09 AM

pls?

#16 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 12 August 2006 - 07:52 AM

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");
I am a PHP Guru, ask me questions if you want to

#17 newb

newb
  • Members
  • PipPipPip
  • Advanced Member
  • 454 posts

Posted 12 August 2006 - 06:32 PM

works! thanks a bunch dude!!1 i was starting to give up on here =[

#18 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 12 August 2006 - 06:36 PM

Well thanks for the big thank you
I am a PHP Guru, ask me questions if you want to




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users