It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming PHP and MySQL Forum

last pix upload doubles when I refresh

Moderator: Malcolm

last pix upload doubles when I refresh

Postby jeaddy on Sat Feb 23, 2008 4:28 am

it seems that the variable $filename is always there and is always the same name of the last uploaded picture. so when you refresh
the page this variable name gets inserted into the sql, again and again and again when you keep refreshing it.

but when you upload a new picture, this variable will have a new name, and then when you refresh this name will be inserted into the database again and again, therefore repeating the pictures (doubling it).

so in essence when you refresh you are running the code to insert the variable name to the sql. we need to change this from happening, if you have any suggestion let me know.
jeaddy
 
Posts: 24
Joined: Thu Dec 20, 2007 12:35 am

Postby flabbyrabbit on Sat Feb 23, 2008 12:19 pm

Well the easiest way to do it would be for the page to be redirected to another page after code has been executed, then if the page is refreshed its not refreshing the code. The other way would be to check the DB to see if an entry has already been submitted.

Flabby Rabbit
Image
User avatar
flabbyrabbit
500+ Club
 
Posts: 706
Joined: Thu Jan 25, 2007 1:10 pm
Location: Midlands, England

Postby jeaddy on Sat Feb 23, 2008 2:05 pm

Well the easiest way to do it would be for the page to be redirected to another page after code has been executed, then if the page is refreshed its not refreshing the code.


for example I use this as redirecting it to another page.

Code: Select all
header("Location: confirmation_page.php");


I tried this, and before uploading anything if you just type localhost/upload.php in the browser this would direct it to the confirmation_page.php.

I don't see this as consistent, so if someone go the this browser it will direct them to confirmation_page.php and then when they upload a picture it will then direct them to confirmation_page.php, they can't really see what they uploaded and they have to go back to localhost/upload.php to view the previous upload pix or reupload another pics.

so how is this consistent?

thanks -
jeaddy
 
Posts: 24
Joined: Thu Dec 20, 2007 12:35 am

Postby flabbyrabbit on Sat Feb 23, 2008 6:33 pm

Well only redirect when the a picture has been uploaded, surely that would solve this??

Flabby Rabbit
Image
User avatar
flabbyrabbit
500+ Club
 
Posts: 706
Joined: Thu Jan 25, 2007 1:10 pm
Location: Midlands, England

Postby jeaddy on Sun Feb 24, 2008 4:31 am

Thanks flabbyrabbit,

the redirecting method works especially if you are posting like entering your name etc, but when you are uploading it is a little different.

I tried it for both and it seems to be working fine for posting when I use this code
Code: Select all
header("Location: " . $_SERVER['PHP_SELF']);


after the insert to the database code. After doing this when I refresh, it doesn't post what was posted before twice. and when I got rid of this code, it does.

But when I do this for picture upload, it is a bit more difficult to do I guess because the complexity of the code.

here is why:

assume the form is created, database is created and connected. Here is the sample code:

Code: Select all
if(isset($_POST['submit'])){
   
       $caption=$_POST['caption'];

mysql_query("insert into upload3(photo, caption) values('$fileName','$caption')");
    header("Location: " . $_SERVER['PHP_SELF']);
     
    }
     
     
     
     

//if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
move_uploaded_file($_FILES['uploaded']['tmp_name'], $target);
//{
echo "The file ".basename( $_FILES['uploaded']['name']). " has been uploaded<br>";
//}
//else
//{
//echo "Sorry, there was a problem uploading your file.";
//}

$data = mysql_query("SELECT * FROM upload3") or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{

Echo"$info[pid]";
Echo "<img src=$dir".$info[photo]." width=200 height=200> <br>";
print"$info[photo]";
}



if I use if(isset($_POST['submit'])){

and have --->}

all the way at the end of the program then header("Location: " . $_SERVER['PHP_SELF']); doesn't work, but if I have it right after inserting the values into the database then I CAN'T upload the pix and view the pic in my browser like how I have the code right now posted.

So do you know how I can solved this?

thanks --
jeaddy
 
Posts: 24
Joined: Thu Dec 20, 2007 12:35 am

Postby jeaddy on Mon Feb 25, 2008 2:51 am

If i input this code
Code: Select all
header("Location: " . $_SERVER['PHP_SELF']);

after this code
Code: Select all
mysql_query("insert into upload3(photo, caption) values('$fileName','$caption')");

this code won't print
Code: Select all
echo "<font color =red> The file </font><b>". basename( $_FILES['uploaded']['name']). "</b> <font color=red>has been uploaded to the</font>". "<b> $dir </b></font>"."<font color=red> directory.</font><br><br>";


Do you know why or how I can fix it?

thanks
jeaddy
 
Posts: 24
Joined: Thu Dec 20, 2007 12:35 am

Postby knowa on Mon Feb 25, 2008 10:47 am

yes, thats because header(location...) does reload the page, and the php code after that will not be executed...
User avatar
knowa
 
Posts: 19
Joined: Sat Dec 08, 2007 4:32 pm
Location: Switzerland


Who is online

Users browsing this forum: No registered users and 0 guests