2016-11-10 113 views
1

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

您的SQL语法错误;检查手册中 对应于你的MySQL服务器版本正确的语法使用 “开”在附近的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");  
    } 
    } 
?> 
+0

你能在这里呼应查询? – Jigar7521

+3

在你的代码中没有工作'on'..确保这段代码是正确的 – scaisEdge

+0

这不好,你有一个SQL注入问题,你打开你的窗体在循环但关闭它的循环(和表...)。在一个地方,你甚至不能使用表单或表单域... – jeroen

回答

0

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

三个步骤,使其工作:

首先,形式前必须周期被创建:

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

接下来,您必须提供DB行(添加值= “$ ID”到复选框)的标识:

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

形式的“动作” php文件,你在FIRST迭代后重定向。移动重定向后的foreach循环:

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

+0

不好,因为我有一个SQL注入或其他的事情??? !!! – m7md

+0

不好,因为它的“混淆”代码没有任何标准。不安全 - 不仅是sqli。 aslo header injection,xss,csrf,... – Martin

+0

你可以请一些教程来学习如何解决这个错误?! – m7md