在每次浏览器刷新时,已经发送到数据库的旧数据会再次发送。如果输入名称'title'有一些值,我想在数据库中插入数据。
这是我的html表单:
<form method="post" enctype="multipart/form-data">
<table>
<tr>
<td><p>Title: <input type="text" name="title"></p></td>
</tr>
<tr>
<td><p>Comment: <input type="text" name="comment" class="inputtext"></p></td>
</tr>
<tr>
<td>
<input type="hidden" name="MAX_FILE_SIZE"value="16000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload"type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
我的PHP代码:
<?php
$con = mysql_connect('','','') or die(mysql_error());
$db = mysql_select_db('db', $con);
if(isset($_POST['upload'])&&$_POST['title']>0)
{
$title = $_POST['title'];
$comment = $_POST['comment'];
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fileType=(get_magic_quotes_gpc()==0 ? mysql_real_escape_string(
$_FILES['userfile']['type']) : mysql_real_escape_string(
stripslashes ($_FILES['userfile'])));
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
if($db){
$query = "INSERT INTO table (title,comment, name, size, type, content ) ".
"VALUES ('$title','$comment','$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed');
mysql_close();
echo "<br>Success: $title <br>";
}else { echo "Error"; }
}
?>