【问题标题】:PHP makes Infinite queryPHP进行无限查询
【发布时间】: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/…

标签: php mysql infinite


【解决方案1】:

执行成功删除查询时,函数 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);
}
...

【讨论】:

    【解决方案2】:

    您应该循环遍历 $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';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-10
      • 2011-07-25
      • 1970-01-01
      • 2016-08-30
      • 2010-09-18
      • 1970-01-01
      • 2013-04-08
      • 2020-09-26
      相关资源
      最近更新 更多