rapidoodle
rapidoodle

Reputation: 340

Deleting selected item

Good day. I want to delete selected item by the user. But it turns out what ever button clicked by user, the first data row is deleting. Im stuck here. Please help.

This is my code:

HTML

    <form  method="POST" action="<?php $_PHP_SELF ?>">
<?php

        $query = "select * from tblsr";
        $request = mysql_query($query)or die(mysql_error());?>
        <table class="table table-hover">
        <tr>    
                <th>Softwares</th>
                <th>Difficulty</th>
                <th></th>
                <th></th>
                </tr>
        <?php while($row = mysql_fetch_array($request)){
        ?>
        <tbody>
            <tr class="success">
                <td><?php echo $row['Software']; ?></td>
                <td><?php echo $row['Difficulty']; ?></td>
                <td><input type="submit" class="btn btn-sm btn-success" name="change" value="Change Difficulty"/></td>  
                <td><input type="submit" class="btn btn-sm btn-danger" name="remove" value="Delete"/></td>
                <input type="hidden" name="id[]" value="<?php echo $row['id']; ?>"/>
            </tr>
        </tbody>
        <?php
        }

?>  </form>

DELETE Query

if(isset($_POST['remove'])){
 $count = count($_POST['id']); //get total number of array element
for ($i = 0; $i < $count; $i++) {
$id = $_POST['id'][$i];
        $sql = "Delete FROM tblsr 
                Where id = '$id'";

            $success = mysql_query($sql) or die (mysql_error());
            mysql_close();  

            if ($success == TRUE){

            ?>
                <script>
                    alert('Deleted.');  
                    window.location.href='manageRequest.php';
                </script>

            <?php
        }

}
    }

Upvotes: 0

Views: 1127

Answers (1)

Alireza Fallah
Alireza Fallah

Reputation: 4607

Your code's problem is that you never increase $i ( $i++ ) , and always your delete query affect on the first row ( id ) . you can put $i++ in your while loop to fix it, but I recommend that you simply do like below and don't bother yourself with a loop.

Delete query :

$rm = $_POST['id'];
if (isset($_POST['remove'])) {
    foreach ($rm as $key => $value) {
        $rm[$key] = "'".$value."'";
    }
    $sql = "Delete FROM tblsr 
                Where id IN(" . implode(",", $rm) .")";
    die($sql);
    $success = mysql_query($sql) or die(mysql_error());

    if ($success) {
        ?>
        <script>
            alert('Deleted.');
            window.location.href = 'manageRequest.php';
        </script>
        <?php

    }

}

your form example for delete users:

<form  method="POST" action="<?php $_PHP_SELF ?>">
    <?php
    $query = "select * from tblsr";
    $request = mysql_query($query) or die(mysql_error());
    ?>
    <table class="table table-hover">
        <tr>    
            <th>Softwares</th>
            <th>Difficulty</th>
            <th></th>
            <th></th>
        </tr>
        <?php
        while ($row = mysql_fetch_array($request)) {
            ?>
            <tbody>
                <tr class="success">
                    <td><?php echo $row['Software']; ?></td>
                    <td><?php echo $row['Difficulty']; ?></td>
                    <td>
                    <input type="checkbox" name="id[]" value="<?php echo $row['id']; ?>"/>
                    delete
                    </td>
            </tr>
            </tbody>
            <?php
        }
        ?> 
    </table>
    <button type="submit" name="remove" class="btn  btn-success">
        delete checked user
    </button>
</form>

Upvotes: 1

Related Questions