PHP 进行无限查询

标签 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);
?>

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

最佳答案

函数 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);
}
...

关于PHP 进行无限查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27766491/

相关文章:

php - 实时和日期插入MySQL服务器

java - 获取 mysql(innodb) AUTO_INCRMENT Column 的方式、JDBC/getGenerateKeys()/last_insert_id (OkPacket 中) 和 LAST_INSERT_ID()

php - 在特定日期之间打开和关闭页面

php - 在 PHP 中内部加入 mongoDB

php - 在 WooCommerce 订单上使用优惠券代码时发送电子邮件通知

javascript - SQL 查询上的 PHP AJAX 日期时间范围问题

php - Laravel Eloquent 获取所有行及其各自的数据透视表行

c++ - 奇怪的循环情况,不知道是什么原因造成的

list - Haskell如何生成这个无限列表?

java - while 循环中未读取 boolean 标志(java)