0

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
        }

}
    }
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
rapidoodle
  • 340
  • 1
  • 3
  • 23
  • Your code is incomplete. I can't see where you really submit the form. – Minoru Jan 24 '14 at 15:12
  • First of all do not put in your page. especially if that is some Front end part where all users can see it. Second of all I do not see that you are using form? And third You should not use mysql_ function. http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – pregmatch Jan 24 '14 at 15:13
  • " First of all do not put in your page. especially if that is some Front end part "(What do you mean by this?) @prematch – rapidoodle Jan 24 '14 at 15:21

1 Answers1

1

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>
Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57