这是我的上传页面,它也会在该页面中显示上传的文件。
每次我按删除键时,都不会让我进入 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();
?>
最佳答案
您的问题在于引号:
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>
关于PHP我无法删除上传的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23170526/