Page 1 of 1

How to delete row if checked

Posted: Mon Sep 01, 2008 1:53 am
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

Re: How to delete row if checked

Posted: Mon Sep 01, 2008 5:02 pm
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.

Re: How to delete row if checked

Posted: Mon Sep 01, 2008 11:46 pm
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>";

Re: How to delete row if checked

Posted: Tue Sep 02, 2008 12:13 pm
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:

Re: How to delete row if checked

Posted: Tue Sep 02, 2008 12:35 pm
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.

Re: How to delete row if checked

Posted: Tue Sep 02, 2008 12:41 pm
by andyhoneycutt
jayshields wrote: I think you can echo comma seperated values as well as concatenated strings.
You most certainly can. =]

-Andy

Re: How to delete row if checked

Posted: Tue Sep 02, 2008 1:17 pm
by califdon
Really?? Ya' learn somethin' new ev'ry day! :oops: Thanks, guys.