【发布时间】:2015-03-02 05:18:32
【问题描述】:
我想从我的服务器中选择一个文件并将其从服务器和数据库中删除。它也可以,但是当它删除时,它会开始无限次执行。所以有一个无限循环,我不明白为什么。
这是我的 delete.php,我在其中选择要删除的文件:
<html>
<body>
<title>Delete your uploads</title>
<?php
session_start();
$username =$_SESSION["uname"];
?>
<form action="deleted.php" method="post">
<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= mysqli_query($con, "SELECT imagesnotes FROM datas where username='$username'");
echo "File or Image";
echo'<select name="imagesnotes">';
echo'<option value="" selected="selected">Select a File</option>';
while($row = mysqli_fetch_array($sql))
{
echo'<option value="' . $row['imagesnotes'] . '">'. $row['imagesnotes'] .'</option>';
}
echo'</select></p><p>';
mysqli_close($con);
?>
<td width="80"><input name="download" type="submit" class="box" id="download" value=" download "></td>
</form>
</body>
</html>
这是我的deleted.php文件,它确认了它:
<?php
session_start();
$username =$_SESSION["uname"];
?>
<?php
$con = mysqli_connect("localhost", "root", "", "webpage");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$imagesnotes = $_POST['imagesnotes'];
$target_dir = "uploads/";
$target_file = $target_dir . basename($imagesnotes);
$sql = mysqli_query($con,"delete from datas where username='$username' and imagesnotes='$imagesnotes'");
while($sql)
{
$delete = unlink($target_file);
if($delete)
{
echo '<br>you deleted your file from our server</br>';
}
else
{
echo 'An error occured';
}
$sql2 = mysqli_query($con,"delete from userdata where username='$username' and imagesnotes='$imagesnotes'");
if(!$sql2)
{
echo "<br>we couldn't delete some of your data from the database please contact administrators.</br>";
}
echo "<br> We deleted your files from server and database. <br>";
}
if(!$sql)
{
echo "<br>There is a problem with connection or our MySQL code, Please contact administrators.</br>";
}
mysqli_close($con);
?>
一切似乎都是真的。你能帮帮我吗?
【问题讨论】:
-
您有一个以
while($sql)开头的循环,但循环内没有任何内容修改$sql- 也许您打算使用if ($sql)? -
是的,谢谢 :)
-
您的代码也不安全。有一些sql注入stackoverflow.com/questions/60174/…