Reputation: 1
This may sound noobish, but i have a code that submits a file to a database (and reads the file) whenever i hit submit, and everything works perfectly, except after i refresh the page it re-adds the last value I Selected. Here is the code where the problem lies :
<?php
mysql_connect("localhost","root","");
mysql_select_db("a1296556_data1");
if(isset($_POST['submit'])){
$name=$_FILES['file']['name'];
$temp=$_FILES['file']['tmp_name'];
move_uploaded_file($temp,"uploaded/".$name);
$url="http://www.bluejayke.com/edit/uploaded/$name";
}
?>
<form action="index.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit" value="Upload">
</form>
<iframe src='video.php' width=250 height=600></iframe>
<?php
if(isset($_POST['submit'])){
mysql_query("INSERT INTO uploadedvideos(id,name,url) VALUES('','$name','$url')");
echo "</br>" . $name . " uploaded";
}
?>
Any input?
Upvotes: 0
Views: 386
Reputation: 7653
First of all stop using mysql_* functions since they've been deprecated, instead start using prepared statements PDO or MySQLi.
1 way of doing this (my preferred way): You need some kind of token which is random/unique and you need to save it in table for form submit.
have hidden input field in your form like this:
<input type="hidden" value="<?=md5(time())?>" name="my_form_token" />
before updating table you must check using select statement if given token already exists in table. If it exists do not update database. if it doest exist then update the table with token and your file.
2nd way of doing this is to redirect after submitting to some other page.
Upvotes: 0
Reputation: 1933
When a form submits, the browser issues a POST
request. When you refresh, the browser issues the last request, thus submitting your form again. However, most browsers will ask you before refreshing after submitting a form. In order to avoid this, you should redirect after a POST.
Upvotes: 2
Reputation: 4634
This is correct functionality. Hitting refresh will re-submit the form and all data with it.
Upvotes: 1