I am trying to create a table with a checkbox for each row to delete the row that checked. Then after the user press the delete button the row that is checked must be deleted.

This is the code that I have so far, but I do not think it's complete yet.


                        <button type="button" onClick="parent.location='groupinfo_edit.php'">Delete</button>

                //LOAD GROUP

                $result = mysql_query("SELECT * FROM `group` ORDER BY group_id ASC") or die(mysql_error());

                <div style="border-color: grey; border-style: solid;">
                <table id="admintable" border="1" cellpadding="2" cellspacing="0" width="800">
                        <th><input type="checkbox" name="all" value=""></th><th>Product Picture</th><th>Product Name</th><th>Product Rate</th><th>Product Size</th><th>Product Price</th><th>Product Info</th><th>Action</th>
                    while ($data = mysql_fetch_array($result)){

                            //echo "<tr bgcolor='$result2'>";                    
                            //echo '<td>'.$data['page'].'</td>';
                            //echo "<td><a href='post.php?post_id=".$data['post_ID']."'><img src='../images/post.jpg'></a></td>";
                            //echo '</tr>';                  

                            echo "<tr bgcolor='$result2'>";
                            echo '<td><input type="checkbox" name="eachproduct" value=""></td>';
                            echo '<td>Picture</td>';
                            echo '<td>Product Name</td>';
                            echo '<td>3 stars</td>';
                            echo '<td>Medium</td>';
                            echo '<td>Price</td>';

                            //LOAD MEMBERS              
                            $resultG = mysql_query("SELECT * FROM `student` INNER JOIN `group`
                            ON student.group_id = group.group_id
                            WHERE `group_name` ='".$data['group_name']."'") or die(mysql_error());

                            echo '<td>Information</td>';                      

                            echo '<td><center><a href="groupmgt.php?group_id='.$data['group_id'].'">Edit</center></td>';
                            echo '</tr>';


Please help me to complete the code. Especially on the checkbox part, how can the computer distinguish if that row is being check and ready to be deleted?

3 Years
Discussion Span
Last Post by mattster

So you need a PHP file that it's going to send to, if the 'delete' submit button has been clicked, and use the checkboxes as an array:

<input name="checkbox[]" type="checkbox" value="<? echo /* row id */ ?>">    
<input type="submit" name="Delete">

         $deleted_id = $_POST['checkbox'];
         $num = count($deleted_id);
         if (count($num) > 0)
             foreach ($deleted_id as $id)
                $sql = "DELETE FROM `db_name` WHERE id='$id'";
                $res = mysql_query($sql) or die(mysql_error());
            echo $num." Records deleted Successfully.";

You should be okay to tweak these snippets to get then working with your code.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.