gpt4 book ai didi

php - 从mysql数据库中删除多行

转载 作者:行者123 更新时间:2023-11-30 22:09:36 25 4
gpt4 key购买 nike

我想使用我的代码中的 php 代码从 mysql 数据库中删除多行,但是我有这条消息错误如何解决这个问题?

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'on' at line 1

$files_query=mysqli_query($conn,"select * from tbl_files where db_isdeleted='1'")or die(mysqli_error($conn));
if(mysqli_num_rows($files_query)>0){
echo"<table class='table table-hover table-responsive table-bordered'><tr>";
echo"<td style='background:#f7ac01;font-size:16px;text-align: center;'><input type='checkbox' class='allcb' data-child='chk'/></td>
<td style='background:#f7ac01;font-size:16px;text-align: center;'>FileName</td>
<td style='background:#f7ac01;font-size:16px;text-align: center;'>Date</td>";
echo"</tr>";
while($row=mysqli_fetch_array($files_query)){
$id=$row['db_id'];
$name=$row['name'];
$date=$row['db_date'];
echo"<tr>";
echo'<form method="post" action="">';
echo"<input type='hidden' name='txt_table' value='tbl_files'>";
echo"<input type='hidden' name='txt_action' value='files'>";
echo"<td style='text-align: center;'><input type='checkbox' name='item[]' class='chk'/></td>";

echo"<td style='text-align: center;'>$name</td>";

echo"<td style='text-align: center;'>$date</td>";

}
echo"</tr>";
echo"</table>";
echo"<input type='submit' name='delete' value='Delete' class='btn btn-danger'>&nbsp;";
echo"<input type='submit' name='restore' value='Restore' class='btn btn-success'>";
echo"</form>";
}

/************************

<?php
if(isset($_POST['delete'])){
$table=$_POST['txt_table'];
$action=$_POST['txt_action'];
foreach($_REQUEST['item'] as $id) {
// delete the item with the id $id
$delete_query=mysqli_query($conn,"DELETE FROM {$table} WHERE db_id=$id")or die(mysqli_error($conn));
header("location:recyclebin.php?action=$action&msg=1");
}
}
?>

最佳答案

对不起我的英语。 :)你的代码有很多“问题”:),但是......

让它发挥作用的三个步骤:

首先,form 必须在 while 循环之前创建:

echo'<form method="post" action="">';    
while($row=mysqli_fetch_array($files_query)){

接下来,您必须提供数据库行的 ID(将 value="$id" 添加到复选框):

echo"<td style='text-align: center;'><input type='checkbox' name='item[]'  value="$id" class='chk'/></td>";

并且在 form "action" php 文件中,您将在 FIRST 迭代后重定向。在每个循环之后移动重定向:

  foreach($_REQUEST['item'] as $id) {
// delete the item with the id $id
$delete_query=mysqli_query($conn,"DELETE FROM {$table} WHERE db_id=$id")or die(mysqli_error($conn));

}
header("location:recyclebin.php?action=$action&msg=1");

但是你的代码真的很糟糕而且不安全,阅读这个:https://en.wikipedia.org/wiki/SQL_injection

关于php - 从mysql数据库中删除多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40525806/

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