How to delete row if checked

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

How to delete row if checked

Post by zplits »

Hi there, good day. Does anyone know how to delete row if the checkbox in html form is checked, as well as delete the data in the database.

here is my code for creating checkbox and every element in my page:

Code: Select all

<?php 
$query = 'Select * from tbl_ingredient';
$result = mysql_query($query) or die('Error in Query');
        
if(mysql_num_rows($result)>0){
    while($row = mysql_fetch_row($result)){
        echo "<table width=\"720\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"gridData\" onMouseOver=\"this.style.backgroundColor='#EDE6D4'\"; onMouseOut=\"this.style.backgroundColor='transparent'\">";
        echo "<tr align = \"center\" class=\"gridRow\">";
        echo "<td width = \"70\"><input type=\"checkbox\" name=\"removeid[]\" id=\"checkbox\" /></td>";
        echo "<td width = \"105\">".$row[1]."</td>";
        echo "<td width = \"202\">".$row[2]."</br></td>";
        echo "<td width = \"140\">".$row[4]."</br></td>";
        echo "<td width = \"153\">".$row[3]."</br></td>";
        echo "<td width = \"59\"><input name=\"Reset\" type=\"submit\" class=\"gridButtons\" id=\"button\" value=\"Edit\" onmouseover=\"this.style.color = '#000000'\" onmouseout=\"this.style.color = '#666666'\"/></td>";
                //echo $row[2]." </br>";
        echo "</tr>";
        echo "</table>";
            }
        }
else{
    echo 'No rows found';
}?>
What will be the right code so that i'll be able to delete those rows which i have checked?
Any help is much appreciated. Thanks
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to delete row if checked

Post by califdon »

The problem is that you are not including the ID of the record to be deleted for each row--they all have the same id: "checkbox". You could make each check box name contain the record id, like: name=\"removeid[".$row[0]."]\" or whatever position the id is in your query. I would advise that you use mysql_fetch_assoc(), though, then extract(), so that you have all the variables with recognizable names, then you could do something like: name=\"removeid[$id]\". In your form data handling code, then you could just go through the $_POST['removeid'] array and you would have the record id's to be deleted.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: How to delete row if checked

Post by Bill H »

The value that is submitted if the checkbox is checked is the "value=x" attribute rather than the "id=x" one. As far as I know the "id" attribute has no real meaning for a checkbox. See: http://www.echoecho.com/htmlforms09.htm

You're creating an array of checkboxes 0-x, but they bear no relation to your records. Califdon's suggestion is one way to do it, in which case you don't need a value for them. You will step through the array using the indexes of the array.

The other would be to go your route but use value attributes and set them equal to the record id. Then step through the array using those values.

Code: Select all

     echo "<td width = \"70\"><input type=\"checkbox\" name=\"removeid[]\" value=\"",$row['id'],"\" /></td>";
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to delete row if checked

Post by califdon »

Excellent point, Bill, I believe you're correct. But I think you meant for those commas to be periods in the last part. :wink:
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: How to delete row if checked

Post by jayshields »

califdon wrote:Excellent point, Bill, I believe you're correct. But I think you meant for those commas to be periods in the last part. :wink:
I think you can echo comma seperated values as well as concatenated strings.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: How to delete row if checked

Post by andyhoneycutt »

jayshields wrote: I think you can echo comma seperated values as well as concatenated strings.
You most certainly can. =]

-Andy
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to delete row if checked

Post by califdon »

Really?? Ya' learn somethin' new ev'ry day! :oops: Thanks, guys.
Post Reply