基本上我创建了一组php文件,它有一个简单的作业:为什么这会将数据多次添加到数据库?
允许用户登录。 允许用户上传文件。 然后允许用户查看他们上传的所有文件。
奇怪的是,当我通过1个用户名上传一个文件时,文件列表结果显示了4次相同的结果,然后我上传了第二个文件,出现了5次。与另一个用户显示它5次。
我检查了文件上传后存储的位置,每个文件只有一个副本。以下是我的代码,有帮助吗?
的index.php - 这已经登录表单,文件上传表单,最后下载列表
<?
break;
}
?>
<?php if ($_SESSION['username']): ?>
<h1>Welcome, <?php echo $_SESSION["username"] ?></h1></br>
<?php
//include ("config.php");
//Connect to mysql server
$link = mysql_connect($host, $dbuser, $dbpwd);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db($dbname);
if(!$db) {
die("Unable to select database");
}
?>
Select File To Upload
<div style="width:100%; margin:5px;">
<form action="uploadclientfile.php" method="post" enctype="multipart/form-data" name="upload" style="margin:5px;">
<label> File</label>
<input name="uploaded_file" type="file" class="input-xlarge" required/>
<input type="hidden" name="" value="<?php $_SESSION['username'] ?>" />
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" /><br /><br />
<input name="Upload" type="submit" value="Upload" class="btn" />
</form>
</div></br></br>
File list:</br>
<?php
$userfiles = mysql_query("SELECT filename, filelocation FROM cfiles WHERE userid='{$_SESSION['username']}'");
while ($row = mysql_fetch_assoc($userfiles)) {
$filename = $row['filename'];
$filelocation = $row['filelocation'];
echo "<a href=".$filelocation .">" .$filename . "</a><br />";
} ?>
<?php endif; ?>
<a href="index.php?action=login">Log-in</a> | <a href="index.php?action=logout">Log-out</a><br />
</body>
</html>
也upload.php的
<?php
session_start();
echo("<pre>");
print_r($_POST);
print_r($_FILES);
echo("</pre>");
$target = "userfiles/";
$target = $target . basename($_FILES['uploaded_file']['name']);
$new_file_name = str_replace(' ', '_', $target);
//This gets all the other information from the form
$userid = $_SESSION['username'];
$file = basename($_FILES['uploaded_file']['name']);
// Cycle through each member and check that it needs to be added to the db
$useruploadids = mysql_query("SELECT id FROM members");
while ($row = mysql_fetch_assoc($useruploadids))
{
//Writes the information to the database
mysql_query("INSERT INTO `cfiles` VALUES ('{$userid}', '{$file}', '{$new_file_name}')") or die(mysql_error()) ;
}
//Writes the file to the server
if(@move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $new_file_name))
{
//Tells you if its all ok
echo "The file ". basename($_FILES['uploaded_file']['name']). " has been uploaded, and your information has been added to the directory.";
}
else
{
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>
因此,有在此使用的2个主要文件处理。任何帮助,为什么我的文件出现在下载列表和MySQL数据库多次?它只在它存储的文件夹中出现一次。
帮助我们通过* a)排除问题范围内的任何无关代码*和* b)正确格式化您的代码* – crush