PHP我无法删除上传的文件

时间:2014-04-19 13:16:29

标签: php mysql url

这是我的上传页面,它还会显示该页面中的上传文件。 每次我按删除,都不会让我到delete.php?id=7。它就像delete.php?id= <

我能知道这个问题吗?

<div id="content">
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1"
cellspacing="1" class="box">
<tr>
<td>Please select a file</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>
 <table>
<?php
 mysql_connect("localhost", "root", "") or die(mysql_error()) ; 
 mysql_select_db("test") or die(mysql_error()) ; 

$sqlquery=mysql_query("SELECT * FROM upload");

while(list($id, $name) = mysql_fetch_array($sqlquery)){
 echo "<tr>";
 echo "<td>".$id."</td>";
 echo "<td>".$name."</td>";
 echo "<td><a href='delete.php?id='".$id."'> delete</a></td>";
 echo "</tr>";
}
?>
</table>
<?php
if(isset($_POST['upload'])&&$_FILES['userfile']['size']>0)
{
$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);
}
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db = mysql_select_db('test', $con);
if($db){
$query = "INSERT INTO upload (name, type, size, content ) ".
"VALUES ('$fileName', '$fileType', '$fileSize', '$content')";
mysql_query($query) or die('Error, query failed'); 
echo '<br/>File' .$fileName. ' uploaded';
mysql_close();
}else { echo "file upload failed";
    }
}
?>
</div>

这是我的delete.php

  <?php

    mysql_connect("localhost","root","")or
    die(mysql_error());
    mysql_select_db("test") or die(mysql_error());
    $sql="DELETE FROM upload WHERE id='$id'";
    $result=mysql_query($sql);
            if(isset($_GET['id'])) {
                $id=$_GET['id'];
                echo 'deleted successfully.';
                echo "<BR>";
                mysql_query("DELETE FROM staff WHERE id = $id");
            header("Location: student_upload.php");
        }else {
    echo "ERROR";   
    }
    ?>
    <?php
    mysql_close();
    ?>

1 个答案:

答案 0 :(得分:1)

你的问题是引用:

echo "<td><a href='delete.php?id='".$id."'> delete</a></td>";

如果id为7,则生成的HTML为:

<td><a href='delete.php?id='7'>delete</a></td>

由于'=之后结束,因此该数字永远不会传递给查询字符串。就像你写了<a href="foo.html" hello there people>click me</a>

要解决此问题,请确保您的引号已正确嵌套:

echo '<td><a href="delete.php?id='.$id.'"> delete</a></td>';

现在您的链接为<td><a href="delete.php?id=7"> delete</a></td>