gpt4 book ai didi

PHP 进行无限查询

转载 作者:行者123 更新时间:2023-11-29 05:19:33 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com