Jump to content

last pix upload doubles when I refresh


johnseito

Recommended Posts

here is the code, 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.

 

<?php
$dir = "pictures/"; 

$target = $dir . basename( $_FILES['uploaded']['name']) ;
//$ok=1; 


$fileName = $_FILES['uploaded']['name'];  


//connects to database
mysql_connect("localhost", "TEST1", "TEST") or die (mysql_error());
mysql_select_db("test") or die (mysql_error());


mysql_query("insert into picture(photo) values('$fileName')");

            
$allowed_type = array('image/jpg','image/tif','image/gif','image/pjpeg','image/png','image/jpeg');

if(in_array($uploaded_type,$allowed_type)){ 


if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
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>";



$data = mysql_query("SELECT * FROM picture") or die(mysql_error());
while($info = mysql_fetch_array( $data ))//Puts it into an array
{

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

}else {
echo "<font color=red> Sorry, there was a problem uploading your file. Please try again!</font><br><br>";
}

}

?>

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.