dan
dan

Reputation: 563

Deleting a row from the database based on checkbox

I'm trying to delete a whole row that contains questiontext and type. The functionality is working as desired in case of deleting. However it's always deleting the final row added and not the ones checked. Any suggestions why?

this is my table structure: enter image description here

case 'Addquiz':

     $sql = "SELECT id,questiontext,type FROM questioninfo ORDER BY type DESC ";

        $result = mysqli_query($con,$sql);
        $selectedtable  = "<form method='post' action=''>\n";
        $selectedtable .= "<table class='sortable'>\n<tr><th>Select</th><th>Question</th><th>Type</th></tr>\n";

        while($row = mysqli_fetch_assoc($result)) { 

$rowID = $row['id']; 
$text = $row['questiontext'];
$type = $row['type']; 

$selectedtable .= "<tr><td><input type='checkbox' name='delete' value='Delete' style='margin:20px;'></td><td><input type='text' name='QuestionText[$rowID]' value='$text' style=' width:600px; text-align:left;'></td><td><select name='Type[$rowID]' style='margin:10px; height:35px'><option selected='selected'></option><option value='$type'>Performace</option><option value='$type'>Loyalty</option></select></td></tr>\n"; 

}
    $selectedtable .= "</table>\n";
    $selectedtable .= "<input type='submit' name='addquestion' value='Add Question' style='width:140px; height:30px; text-align:center; padding:0px;'>\n";    
    $selectedtable .= "<input type='submit' name='submit' value='Update' style='width:80px; height:30px; text-align:center; padding:0px;'>\n";
    $selectedtable .= "<input type='submit' name='del' value='Delete' style='width:80px; height:30px; text-align:center; padding:0px;'>\n";
    $selectedtable .= "</form>\n";

            if(isset($_POST['submit']))

            {   
                foreach($_POST['QuestionText'] as $rowID => $text)
                { 

                $sql = "UPDATE questioninfo SET questiontext = '$text', type = '$type' WHERE id = '$rowID'"; 
                mysqli_query($con,$sql);


                } 

            }

            if(isset($_POST['addquestion']))
            {   

                $sql="INSERT INTO `questioninfo` (`ID`) VALUES (NULL)";
                mysqli_query($con,$sql);


            }

            if(isset($_POST['del']))
            {   

                $sql="DELETE FROM questioninfo  WHERE id = '$rowID'";
                mysqli_query($con,$sql);


            }


break;

Upvotes: 1

Views: 55

Answers (2)

Master_ex
Master_ex

Reputation: 779

This is a simplified example of a possible way of doing what you want:

<?php $values = array( 1, 2, 3 ); ?>

<form method="post">
    <?php foreach($values as $value) {
        echo "<input type='checkbox' name='delete$value' value='Delete'/>"; 
    }?>
    <input type="submit" name="submit"/>
</form>

<?php
if(isset($_POST['submit'])) {
    foreach($values as $value) {
        if(isset($_POST["delete$value"]) && $_POST["delete$value"]) {
            echo "delete$value is true";
        } else {
            echo "delete$value is false";
        }
        echo "<br/>";
    }
}
?>

In your code this could be done like that:

...
$selectedtable .= "<tr><td><input type='checkbox' name='delete$rowID' value='Delete' style='margin:20px;'></td><td><input type='text' name='QuestionText[$rowID]' value='$text' style=' width:600px; text-align:left;'></td><td><select name='Type[$rowID]' style='margin:10px; height:35px'><option selected='selected'></option><option value='$type'>Performace</option><option value='$type'>Loyalty</option></select></td></tr>\n"; 
...
if(isset($_POST['del']))
{   
      while($row = mysqli_fetch_assoc($result)) {
         $rowID = $row['id'];
         if(isset($_POST["delete$rowID"]) && $_POST["delete$rowID"]) {
             $sql="DELETE FROM questioninfo  WHERE id = '$rowID'";
             mysqli_query($con,$sql);
         }
      }


}

However, this way requires to make a second query to the database on delete to get all the row id's. The optimal way is to post an array of checkboxes from the form as Shailesh implies in his solution.

Upvotes: 0

Shailesh Katarmal
Shailesh Katarmal

Reputation: 2785

<?php
if(isset($_POST['submit']))
{
    $checkbox=$_POST['chk'];
    for($i=0;$i<count($checkbox);$i++)
    {
        $id = $checkbox[$i];

        $sql1 = "DELETE  FROM manufacturer  WHERE id ='$id' ";
        mysql_query($sql1) or die(mysql_error());
    }
}
?>
<form name="unit" method="post">
<table width="100%" cellpadding="0" cellspacing="0" >
<?php
$selUnit = "select id, name from manufacturer order by id desc limit 20";
$rsUnit   =  mysql_query($selUnit);
$rows     =  mysql_num_rows($rsUnit);
if($rows>0)
{
    while($arrUnit = mysql_fetch_array($rsUnit))
    {
        ?>
        <tr >
            <td >
                <input type="checkbox" name="chk[]" id="chk[]" value="<?=$arrUnit["id"]?>">
            </td>
            <td width="54%" align="left">
                <?=$arrUnit["name"]?>
            </td>   
        </tr>
        <?php
    }
}
?>
    <tr>
        <td colspan="2" align="center">
            <input type="submit" name="submit" value="submit" >
        </td>
    </tr>
</table>

</form>

Upvotes: 1

Related Questions