I'm so sorry for my bad english and such Basic Question. I would like to ask:
Assuming I have displayed table with 6 columns and rows of values. At the end of columns I have "Option Title" and rows have Option as "DELETE Button".
Like this: Complete table
What I want is when I click the Delete button all values on rows selected are completely deleted.
Like this: After delete
Here is my PHP and HTML code:
<tbody>
<tr>
<?php
while($getdata = mysqli_fetch_array($result))
{
echo"<td>".$getdata['x_id']."</td>";
echo"<td>".$getdata['x_title']."</td>";
echo"<td>".$getdata['x_track']."</td>";
echo"<td>".$getdata['x_tag']."</td>";
echo"<td>".$getdata['x_model']."</td>";
echo'<td><img class="img-table-thumbnail" src="'.$getdata['x_thumb'].'"></td>';
echo'<td><a type="button" class="btn btn-sm btn-default" href="'.$getdata['x_link'].' " target="_BLANK">VISIT</a></td>';
echo'<td><form action="delete.php" method="post" enctype="multipart/form-data">
<button type="submit" name="deleteData" class="btn-sm btn-primary"><i class="fa fa-times-circle" aria-hidden="true"></i></button>
</form></td>';
echo "</tr>";
}
//$result = mysqli_query($db_connect,$sql);
$db_connect->Close();
?>
</tbody>
and delete.php:
$id = (int)$_GET['id'];
$update = $db_connect->prepare("DELETE FROM $db_table WHERE id = ?");
$update->bind_param('i', $id);
$update->execute();
$update->close();
header("location:index.php");
?>
Looking for help and thanks in advance for any answers.
Please try with the below code:
NOTE: I haven't seen the complete code, but I have changed the code where there is wrong assignments/declarations. you can feel free to adjust the code
<tbody>
<tr>
<?php
while($getdata = mysqli_fetch_array($result))
{
echo"<td>".$getdata['x_id']."</td>";
echo"<td>".$getdata['x_title']."</td>";
echo"<td>".$getdata['x_track']."</td>";
echo"<td>".$getdata['x_tag']."</td>";
echo"<td>".$getdata['x_model']."</td>";
echo'<td><img class="img-table-thumbnail" src="'.$getdata['x_thumb'].'"></td>';
echo'<td><a type="button" class="btn btn-sm btn-default" href="'.$getdata['x_link'].' " target="_BLANK">VISIT</a></td>';
echo'<td><form action="delete.php" method="post" enctype="multipart/form-data">
<button type="submit" name="deleteData" class="btn-sm btn-primary" value=".$getdata['x_id']."><i class="fa fa-times-circle" aria-hidden="true"></i></button>
</form></td>';
echo "</tr>";
}
//$result = mysqli_query($db_connect,$sql);
$db_connect->Close();
?>
Delete.php
$id = (int)$_POST['deleteData'];
$update = $db_connect->prepare("DELETE FROM $db_table WHERE id = ?");
$update->bind_param('i', $id);
$update->execute();
$update->close();
header("location:index.php");
?>
You are using POST as requestmethod on delete.php. Doing so, you get your id by $_POST['id'] and not by $_GET['id']. But in your case you do not have any form control that holds that value.
Now you have two options.
1.) Change your form by renaming the button to "id" and set it's value attribute to $getdata['your_field_with_id']. Inside delete.php you than have to change $_GET['id'] into $_POST['id'] because you're sending the formdata by POST and not GET.
echo '
<td>
<form action="delete.php" method="post" enctype="multipart/form-data">
<button type="submit" name="id" value="', $getdata['your_field_with_id'], '" class="btn-sm btn-primary">
<i class="fa fa-times-circle" aria-hidden="true"></i> DELETE
</button>
</form>
</td>';
2.) Replace the col with your form with the following code
echo '
<td>
<a href="delete.php?id=', $getdata['your_field_with_id'], '" class="btn btn-sm btn-primary">
<i class="fa fa-times-circle" aria-hidden="true"></i> DELETE
</a>
</td>';
As you can see I remove the form an replace it by by a simple link, that holds the id of the record.
Personally, I would prefer the second solution over the first.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.