2014-01-28 69 views
-1

基本上我创建了一组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数据库多次?它只在它存储的文件夹中出现一次。

+0

帮助我们通过* a)排除问题范围内的任何无关代码*和* b)正确格式化您的代码* – crush

回答

3

你的这部分代码:

// 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()) ; 
} 

遍历你怎么过许多用户并增加了对每个新的文件条目。因此,如果您有5个用户,则可以为cfiles添加5个条目,用于登录该用户的$userid。随着用户数量的增加,这将增加。

这是你的意思吗?您可能只想为该用户添加一个条目,而不是其他用户,请更正此问题?

如果除去循环和更换与此代码:

mysql_query("INSERT INTO `cfiles` VALUES ('{$userid}', '{$file}', '{$new_file_name}')") or die(mysql_error()) ; 

你只能得到一个条目

+1

我的想法正是如此。 – Josh

+0

啊哈。我使用了另一个上传表单中的代码片断,这个表单使用了多个勾选框,因此多个用户可以拥有相同的文件。这就说得通了。所以如果我删除它会解决问题? – Gary

+0

只删除'SELECT'和'while'循环 – Josh

1

此代码混淆了我:

// 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()) ; 
} 

你到底在这里做?看起来您要将上传的文件多次插入到数据库中,对于存在的每个用户来说都是一次。你为什么这样做?这就是为什么文件出现多次? (看起来很可能是我)

+0

嘿,这真的是一个答案? –

+0

问题不明@FélixGagnon-Grenier。我相信这是他问题的根源。我比其他答案落后了几秒,这本质上是一回事。 – Josh

+0

是的,它是;看到[这个评论](http://stackoverflow.com/questions/21416346/why-does-this-add-data-to-the-database-multiple-times/21416503#comment32308335_21416491):*是的,只是删除了位循环,以及括号,看起来像现在的作品* – Josh

相关问题