php - Delete multiple rows from mysql database -
i want delete multiple rows mysql database using php code code have message error how solve ?
you have error in sql syntax; check manual corresponds mysql server version right syntax use near 'on' @ line 1
$files_query=mysqli_query($conn,"select * tbl_files 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'> "; 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'] $id) { // delete item id $id $delete_query=mysqli_query($conn,"delete {$table} db_id=$id")or die(mysqli_error($conn)); header("location:recyclebin.php?action=$action&msg=1"); } } ?>
sorry english. :) code has many "problems" :), but...
three steps make works:
first, form must created before while cycle:
echo'<form method="post" action="">'; while($row=mysqli_fetch_array($files_query)){
next, must provide ids of db rows (add value="$id" checkboxes):
echo"<td style='text-align: center;'><input type='checkbox' name='item[]' value="$id" class='chk'/></td>";
and in form "action" php file, redirecting after first iteration. move redirect after foreach cycle:
foreach($_request['item'] $id) { // delete item id $id $delete_query=mysqli_query($conn,"delete {$table} db_id=$id")or die(mysqli_error($conn)); } header("location:recyclebin.php?action=$action&msg=1");
but code bad , unsafe, read this: https://en.wikipedia.org/wiki/sql_injection
Comments
Post a Comment