PHP进行无限查询

时间:2015-01-04 14:25:59

标签: php mysql infinite

我想这样做我想从我的服务器中选择一个文件并从服务器和数据库中删除它。它也有,但当它删除时,它开始无限次地做。所以有一个无限循环,我不明白为什么。
这是我的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);
?>

一切似乎都是真的。你能帮帮我吗?

2 个答案:

答案 0 :(得分:2)

执行成功的删除查询时,Funcion mysqli_query始终返回TRUE。这是无限循环的原因。

当您有单个文件名时($ _POST ['imagesnotes']),您不需要循环结果。

...
$sql = mysqli_query($con,"delete from datas where username='$username' and  imagesnotes='$imagesnotes'"); // $sql = TRUE on success
$delete = false;
if (file_exists($target_file)) {
    $delete = unlink($target_file);
}
...

答案 1 :(得分:1)

你应该遍历$ sql结果的行数。

$sql = mysqli_query($con,"delete from datas where username='$username' and  imagesnotes='$imagesnotes'");

$loop = mysqli_num_rows($sql);

for ($i=0, $i <= $loop, $i++) {

$delete = unlink($target_file);

if($delete) {
echo '<br>you deleted your file from our server</br>';`
}
else 
{
echo 'An error occured';
}