How to delete checked row from db - php

I have this deleting sql but can't seem to find out why it's not working :/
After trying to delete a checked row the if($result) does reload the page but the row is still there ... :/
Can someone please help me ?
What am I missing ?
<form name="delete_posts" method="post" action="">
<table class="all_posts">
<tr>
<td class="checkbox"><input type="checkbox" name="select_all" id="select_all" /></td>
<td class="post_title">Title</td>
<td>Auther</td>
<td>Posted</td>
<td>Updated</td>
<td>Category</td>
</tr>
<tr><td class="delete_posts" colspan="6"><input name="delete" type="submit"
id="delete" value="delete"></td></tr>
<?php
$post_set = get_all_posts();
while ($row = mysqli_fetch_array($post_set)){
echo "<tr>
<td><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=".$row['id']."></td>
<td><a href=\"edit_post.php?edit=".$row['id']."\">" .$row['title']. "</td>
<td>Auther</td>
<td>".date("d. M. 'y", strtotime($row["date"]))."</td>
<td>Update Date</td>
<td>".str_replace("_"," ",$row['category'])."</td>
</tr>";}
// Delete Posts
if(isset($_POST['delete'])){
$checkbox = $_POST['delete'];
$count = count($checkbox);
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM posts WHERE id = $del_id ";
$result = mysqli_query($connection, $sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=posts.php\">";
}
}
?>
</table>
</form>
Thanx for helping
Cheers
Chris

I think you have an error in your code where you retrive the checked row : instead of getting the checkbox values ($checkbox = $_POST['checkbox'];) you are getting the delete value ($checkbox = $_POST['delete'];).
Here is the corrected part :
if(isset($_POST['delete'])){
$checkbox = $_POST['checkbox'];
$count = count($checkbox);
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM posts WHERE id = $del_id ";
$result = mysqli_query($connection, $sql);
}

Related

delete from MySQL database using checkboxes

I'm attempting to delete a row from a table with data that is generated from a MySQL table. I took a look at both of these questions:
1. How to delete rows of database results using checkbox
2.Deleting multiple rows using checkboxes, PHP and MySQL.
I need help for code delete using checkboxes..
<?php
$sql = mysqli_query($conn, "SELECT * FROM leads ORDER BY lid ASC");
if(mysqli_num_rows($sql) == 0){
echo '<tr><td colspan="8">No Data Entry</td></tr>';
}else{
while($row = mysqli_fetch_assoc($sql)){
echo '<tr>
<td> <input name"checkbox[]" value"'.$row['lid'].'" type="checkbox"></td>
<td>'.$row['name'].'</td>
<td>'.$row['sex'].'</td>
<td>'.$row['phone'].'</td>
<td>'.$row['company'].'</td>
<td>'.$row['vehicle'].'</td>
</tr>';
}
}?>
Cancel
<?php
$del_lid = $_POST['checkbox'];
if (isset($_POST['submit'])) {
foreach($del_lid as $value){
$sql = "DELETE FROM leads WHERE lid='".$value."'";
$result = mysqli_query($conn, $sql);
}
}
?>
The checkboxes were missing the equals sign so effectively none of them had a value. The sql could be streamlined to use the in operator rather than a loop.
<table>
<?php
$sql = mysqli_query($conn, "SELECT * FROM leads ORDER BY lid ASC");
if( mysqli_num_rows($sql) == 0 ){
echo '<tr><td colspan="8">No Data Entry</td></tr>';
}else{
while($row = mysqli_fetch_assoc($sql)){
echo '
<tr>
<td> <input name="checkbox[]" value="'.$row['lid'].'" type="checkbox"></td>
<td>'.$row['name'].'</td>
<td>'.$row['sex'].'</td>
<td>'.$row['phone'].'</td>
<td>'.$row['company'].'</td>
<td>'.$row['vehicle'].'</td>
</tr>';
}
}
?>
</table>
<input type="submit" name="delete" class="btn btn-sm btn-primary" value="delete">
Cancel</td>
</form>
</div>
<?php
$del_lid = $_POST['checkbox'];
if ( isset($_POST['submit'] ) ) {
$sql='delete from `leads` where `lid` in ( ' . implode( ',', $del_lid ). ' )';
$result = mysqli_query($conn, $sql);
}
?>

Cannot delete row using check box

I'm trying to delete a row from table which is connected to database using checkbox (and it wouldn't matter if its just one row or multiple rows), but it is not working, as in nothing happens. It doesn't delete, no errors or warnings appear just a refresh.
php:
<?php
if(isset($_POST['del_event']))
{
if(isset($_POST['check']) && count($_POST['check']))
{
$array = array("check" => $_POST['check']);
$id = implode(',', $array['check']);
$result = mysql_query("DELETE FROM event WHERE event_id = '$id'") or die(mysql_error());
}
}
?>
html:
<form class="buttons" method="post" action="event.php">
<div class="button-wrap">
<input type="button" id="add_event" name="add_event" value="Add Event"/>
<input type="submit" id="del_event" name="del_event" value="Delete Event"/>
</div>
</form>
<tbody class="tbody-event-list-table2">
<?php
require "connection.php";
$check = mysql_query("SELECT * FROM event") or die(mysql_error());
if(mysql_num_rows($check) > 0)
{
while($row = mysql_fetch_array($check))
{
$id = $row['event_id'];
$name = $row['event_name'];
$start = $row['start'];
$end = $row['end'];
$venue = $row['event_venue'];
echo
"<tr>
<td><input type='checkbox' name='check' id='check' class='check' value='$id'/><a href=''
class='event-link'>$name</a></td><td>$start</td><td>$end</td><td>$venue</td>";
echo "</tr>";
}
}
else
{
echo
"<tr>
<td colspan='4'>There Are No Events.</td>
</tr>";
}
?>
</tbody>
It is if(isset($_POST['del_event'])) because 'del_event' is a name of a delete button which is disabled unless a checkbox is checked.
<script>
var chkbox = $(".check"),
button = $("#del_event");
button.attr("disabled","disabled");
chkbox.change(function(){
if(this.checked){
button.removeAttr("disabled");
}else{
button.attr("disabled","disabled");
}
});
</script>
php version is 5.5.9
This would still give you an undefined error if it is the check variable
if (isset($_POST['check']) && count($_POST['check']) != 0)
but if a checkbox is off in a form it is not sent with the form so it does not exist.
Carpetsmoker was halfway right, a proper solution would be:
if (isset($_POST['check'])) {
if ( (int) $_POST['check'] !== 0) {
}
}
btw, it is better to always cast your variables to a propert type you are checking for.
In PHP 0 can be 0, false or null.
Prevent this by safe type checking with the ===
so i fixed the prob. first i had to remove "form" that was separating the buttons and the table so now "form" is wrapped around both buttons and table. second is the php coding for deletion has changed too.
<form class="buttons" method="post" action="event.php">
<div class="button-wrap">
<input type="button" id="add_event" name="add_event" value="Add Event"/>
<input type="submit" id="del_event" name="del_event" value="Delete Event"/>
<input type="submit" id="edit_event" name="edit_event" value="Edit Event">
</div>
<div class="event-list-table2" >
<table class="event-table">
<thead>
<tr>
<th>Event Name</th>
<th>Start Event</th>
<th>End Event</th>
<th>Venue</th>
</tr>
</thead>
<tbody class="tbody-event-list-table2">
<?php
require "connection.php";
$check = mysql_query("SELECT * FROM event") or die(mysql_error());
if(mysql_num_rows($check) > 0)
{
while($row = mysql_fetch_array($check))
{
$id = $row['event_id'];
$name = $row['event_name'];
$start = $row['start'];
$end = $row['end'];
$venue = $row['event_venue'];
echo
"<tr>
<td><input type='checkbox' name='check[]' class='check' value='$id'><a href='' class='event-link' value='$id' name='event-link'>$name</a></td><td>$start</td><td>$end</td><td>$venue</td>";
echo "</tr>";
}
}
else
{
echo
"<tr>
<td colspan='4'>There Are No Events.</td>
</tr>";
}
?>
</tbody>
<?php
if (isset($_POST['del_event']) && isset($_POST['check']))
{
foreach($_POST['check'] as $del_id)
{
$del_id = (int)$del_id;
$sql = mysql_query("DELETE FROM event WHERE event_id = $del_id") or die(mysql_error());
if($sql)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=event.php\">";
}
}
}
?>
</table>
</div>
</form>

ANSWERED: Form is updating database correctly

This code now works to update each row of data individually if submit button is clicked.
Original issue was that I could not get each record updated individually and it was updating ALL rows instead of just the one matching the ID I wanted.
CONNECTIONS STUFF
<form method='post'>";
$query="SELECT * FROM table WHERE approved='no'";
$result = mysql_query($query) or die(mysql_error());
$count = mysql_num_rows($result);
echo "<p>$count pending approval.</p>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id=$row['id'];
$name = $row['name'];
$extra = $row['extra'];
echo "
<table>
<tr>
<td>ID:</td>
<td>$id <input type='hidden' name='id[]' value='$id'></td>
</tr>
<tr>
<td>Name:</td>
<td>$name <input type='hidden' name='name[]' value='$name'></td>
</tr>
<tr>
<td>Extra:</td>
<td>$extra <input type='hidden' name='extra[]' value='$extra'></td>
</tr>
<tr colspan='2'>
<td>
<center><input name='submit' type='submit' value='Approve'></form></center>
</td>
</tr>
</table><br>
";}
if($_POST['submit']) {
$update = "UPDATE table SET approved='yes' WHERE id='$id' LIMIT 1";
if(mysql_query($update)) $count++;
else die("Error in query:<br>$sql<br>");
echo "<p><b>$name has been approved</b></p>";
}
?>
You have to move your update statement outside the while (($i < $num)) {...}.
Currently, that's inside the loop...
You are looping over each row, and then checking if the submit button was clicked, and if so updating the row.
The issue is that you dont identify which button was clicked and so each row is updated when any button is pressed. Try this:
if (isset($_POST['accepted']) && isset($_POST['id']) && $_POST['id'] == $id)
This will check to see if the submited form corresponds to the current row
The fault is in here:
...
<?php
if (isset($_POST['accepted'])) {
$query_update = "UPDATE mytable SET accepted='yes' WHERE id ='$id'";
$result_update=mysql_query($query_update);}
$i++;
}
mysql_close();
?>
....
$i is the run vairable to iterate over ALL rows. it only gets incremented when $_POST['accepted'] is set. And in this particular case, it's generateing an update for each and evry single row with an $id which has come from the databse instead of the current POST.
Thus: all records will be updated.
Modfify:
...
<?php
if (isset($_POST['accepted']) && isset($_POST['id']) ) {
$updateId = $_POST['id'];
$query_update = "UPDATE mytable SET accepted='yes' WHERE id ='$updateId '";
$result_update=mysql_query($query_update);
mysql_close();
}
$i++;
?>
....

Passing variable via submit button

Im loading content for my website from database. It loads data and fills a table.
My problem is that i have put a button next to each row. When i click on the button it has to show me the Name, Price ,Stock etc of every row.
When i click on the button i get an error.
Here you can find the code i have written in de document User_Koeken.php
<?php
if (isset($_POST[$ID]))
{
$URL = $_POST['S_URL'];
$Naam = $_POST['S_Naam'];
$Inhoud = $_POST['S_Inhoud'];
$Stock = $_POST['S_Stock'];
$Prijs = $_POST['S_Prijs'];
echo $URL."".$Naam."".$Inhoud."".$Stock."".$Prijs;
}
else
{$supermarket = mysql_connect("localhost", "root", "Password") or die(mysql_error());
mysql_select_db("supermarket", $supermarket);
$sql = " select * from koeken";
$result = mysql_query($sql, $supermarket);
while ($row = mysql_fetch_array($result)) {
$ID = $row['ID'];
$URL = $row['URL'];
$Naam = $row['Naam'];
$Inhoud = $row['Inhoud'];
$Stock = $row['Stock'];
$Prijs = $row['Prijs'];
$_SESSION['ID']=$ID;
echo "<form action='User_Koeken.php' method='post'>
<tr class='rien' >
<td name='S_URL'><a href=$URL><img src=$URL alt='product'></a></td>
<td name='S_Naam'>$Naam</td>
<td Name='S_Inhoud'>$Inhoud</td>
<td name='S_Stock'>$Stock</td>
<td name='S_Prijs'>€ $Prijs</td>
<td><input type='submit' value=$ID name='$ID'></td>
</tr>
</form> ";
}; }?>
Here you can find a picture of my code in color
http://postimg.org/image/n6wrb0d13/
http://postimg.org/image/n6wrb0d13/
$ID must be initialized before the line
if (isset($_POST[$ID]))
Otherwise, this will always return false, or an error.
Instead, use an array in your HTML names to catch the row being posted:
<tr class='rien' >
<td name='myform[S_URL]'><a href='$URL'><img src='$URL' alt='product'></a></td>
<td name='myform[S_Naam]'>$Naam</td>
<td Name='myform[S_Inhoud]'>$Inhoud</td>
<td name='myform[S_Stock]'>$Stock</td>
<td name='myform[S_Prijs]'>€ $Prijs</td>
<td><input type='submit' value='$ID' name='myform[id]'></td>
</tr>
and the PHP:
if (isset($_POST['myform']) ) {
$post = $_POST['myform'];
$ID = $post['id'];
$URL = $post['S_URL'];
....
}

Delete selected row from table in php

I am using the code (pasted below) to delete the record from the table when it's selected by the checkbox. It is working, but it is not deleting the record from the table, it is only showing the echo "Records deleted Successfully.".
Please have a look at my code and suggest me some changes.
<?php
echo "Hiiiiiiiiii";
include("conn.php");
$sql="select * from test ";
$res=mysql_query($sql) or die(mysql_error());
?>
<form name="form1" method="POST" action="">
<table width="578" border="1" align="center" id="menu">
<tr>
<th></th>
<th>id</th>
<th>Name</th>
<th>email</th>
<th>phno</th>
</tr>
<?php
while($row=mysql_fetch_array($res))
{
?>
<tr>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['emailid'];?></td>
<td><?php echo $row['phno'];?></td>
<?php
echo"<td><a href='update.php?id=".$row['id']."'>Update</a></td>";
?>
<?php
}
?>
<tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>
<?php
// Check if delete button active, start this
$count = mysql_num_rows($res);
echo "$count";
if(isset($_POST['delete']))
{
$delete_id = $_POST['checkbox'];
$id = count($delete_id );
if (count($id) > 0)
{
foreach ($delete_id as $id_d)
{
$sql = "DELETE FROM `test` WHERE id='$id_d'";
$delete = mysql_query($sql);
}
}
if($delete)
{
echo $id." Records deleted Successfully.";
}
}
>?
Add error_reporting(E_ALL); to the top of your file. Now you will be able to see all errors. http://php.net/manual/en/function.error-reporting.php
Also, use var_dump() to check what is actually in your variables. When you run var_dump($_POST); you can clearly see what is actually in your post.
http://php.net/manual/en/function.var-dump.php
if(isset($_POST['delete'])){
$delete = false;
$ids = array();
foreach($_POST['checkbox'] as $val){
$ids[] = (int) $val;
}
$ids = implode("','", $ids);
$sql = "DELETE FROM `test` WHERE id IN ('".$ids."')";
$delete = mysql_query($sql);
$id = mysql_affected_rows();
if($delete){
echo $id." Records deleted Successfully.";
}
}
also your check-boxes should have:
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id']; ?>">
Put the delete code before selecting the records, then only you can see the actual records available in the DB.
<?php
echo "Hiiiiiiiiii";
include("conn.php");
if(isset($_POST['delete']))
{
$delete_id = $_POST['checkbox'];
$id = count($delete_id );
if (count($id) > 0)
{
foreach ($delete_id as $id_d)
{
$sql = "DELETE FROM `test` WHERE id='$id_d'";
$delete = mysql_query($sql);
}
}
if($delete)
{
echo $id." Records deleted Successfully.";
}
}
$sql="select * from test ";
$res=mysql_query($sql) or die(mysql_error());
?>
<form name="form1" method="POST" action="">
<table width="578" border="1" align="center" id="menu">
<tr>
<th></th>
<th>id</th>
<th>Name</th>
<th>email</th>
<th>phno</th>
</tr>
<?php
while($row=mysql_fetch_array($res))
{
?>
<tr>
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['emailid'];?></td>
<td><?php echo $row['phno'];?></td>
<?php
echo"<td><a href='update.php?id=".$row['id']."'>Update</a></td>";
?>
<?php
}
?>
<tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>
<?php
// Check if delete button active, start this
$count = mysql_num_rows($res);
echo "$count";
>?
<? echo $rows['id']; ?>
there is no $rows here remove the s put
<? echo $row['id']; ?>
You should name the checkboxes something like
name="checkbox[<?php echo $row['id'] ?>]"
And then when you're trying to delete
foreach ($_POST['checkbox'] as $id => $delete) {
// delete id
}
Your way of selecting an id by count is rather awkward, you're also only deleting 1 row, which is apparently non-existant because the count is not an id.
Also, the value of a checkbox is meant to be an indicator off the checkbox being ticked or not, it's not usually holding a value, the usual way is to make the name hold what the checkbox actually means.
There is a Typo in the code that outputs the id:
... value="<? echo $rows['id']; ?>">
It should be
... value="<? echo $row['id']; ?>">
Note that your code is also vulnerable to SQL-injection attacks. You should check if the ids in the POST-request are really integers.
you should move this code below of include("conn.php"); in your code
if (isset($_POST['delete'])) {
$delete_id = $_POST['checkbox'];
$id = count($delete_id);
if (count($id) > 0) {
foreach ($delete_id as $id_d) {
$sql = "DELETE FROM `test` WHERE id='$id_d'";
$delete = mysql_query($sql);
}
}
if ($delete) {
echo $id . " Records deleted Successfully.";
}
}
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id'];?>"></td>
<td><?php echo $row['id'];?></td>
Need to change as below to work it perfectly:
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="**<?php** echo $rows['id']; ?>"></td>

Categories