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>";
}

}

?>

I did use POST.

 

 

<form enctype="multipart/form-data" action="upload.php" method="POST">
Please choose a file: <input type="file" name="uploaded"  /><br />
<input type="submit" name="submit" value="Upload" />
</form>

 

could you provide an example?

 

thanks -

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.