Hello, I create this backend system. When someone click check box then press apply. This script suppose to delete all rows that are checked. How to do so? It seems like the script are incomplete.

<?php
    $i=0;
    while ($data = mysql_fetch_array($result)){
    $result2=($i%2)?'#DFA09D':'white';
        echo "<tr bgcolor='$result2'>";
        echo '<td><input type="checkbox" name="vehicle" value="Bike"></td>';
        echo '<td>'.$data['productname'].'</td>';
        echo '<td>'.$data['location'].'</td>';
        echo '<td>'.$data['description'].'</td>';
        echo '<td>'.$data['urlimages'].'</td>';
        echo '<td><a href="product.php?mode=delete&id='.$data['id'].'">Hapus</a> |<a href="input_image.php?id='.$data['id'].'">Edit</a></td>';
        echo '</tr>';
    $i++;
    }
    ?>
    </table>

    <br>
    <tr>
    <td><select name="lokasi">
    <option value="Show">Show</option>
    <option value="DoNotShow">Do Not Show</option>
    <option value="Delete">Delete</option>
    </select></td>
    <td><input type="submit" name="ok" value="Apply"/></td>
    </tr>
    </form>


    </div>

Recommended Answers

All 10 Replies

Yes, this is missing a lot. Is this all you got?

yes, what else do I need to add?

is this script part of product.php?

missing the that prosses the results
The part with either $_POST, $_GET, or $_REQUEST

yes, it's a part of product.php. How to code it?

Member Avatar for LastMitch

yes, it's a part of product.php. How to code it?

There should be a delete query that is connected to those checkboxes.

<a href="product.php?mode=delete&id='.$data['id'].'">

The delete query will delete that id

I don't know what post so far but on this thread you don't have a query to do that but you have a link to delete that id.

Did you write any of this at all?

first of all you need to change the name of the checkbox, for now it becomes "vechicle" at every time.
so you need to change your whole code as following

<?
    if(isset($_POST['submit'])
    {
        $del = 0;
        for($i=0; $i<total_no_rows; $i++)
        {
            $delete_id = 'vehicle_'.$i
            if($_POST[$delete_id] == 1)
            {
                mysql_query("YOUR DELETE QUERY");
                 $del++;
            }
        }
        Echo 'Your total '.$del.' Records are deleted';
}


<form name="delete_form" id="delete_form" method="post" action="">
<?php
    $i=0;
    while ($data = mysql_fetch_array($result)){
    $result2=($i%2)?'#DFA09D':'white';
        echo "<tr bgcolor='$result2'>";
        echo '<td><input type="checkbox" name="**vehicle_'.$i.'**" value="1"></td>';
        echo '<td>'.$data['productname'].'</td>';
        echo '<td>'.$data['location'].'</td>';
        echo '<td>'.$data['description'].'</td>';
        echo '<td>'.$data['urlimages'].'</td>';
        echo '<td><a href="product.php?mode=delete&id='.$data['id'].'">Hapus</a> |<a href="input_image.php?id='.$data['id'].'">Edit</a></td>';
        echo '</tr>';
    $i++;
    }
    ?>

    <tr>
    <td><select name="lokasi">
    <option value="Show">Show</option>
    <option value="DoNotShow">Do Not Show</option>
    <option value="Delete">Delete</option>
    </select></td>
    <td><input type="submit" name="ok" value="Apply"/></td>
    </tr>
    </table>
    </form>

First, create the form like so;

<form method="post" target="some/php/script.php">
    <input type="checkbox" name="item[]" value="1" />
    <input type="checkbox" name="item[]" value="2" />
    <input type="checkbox" name="item[]" value="3" />
    <input type="submit" name="delete" value="Delete checked items" />
</form>

Note that the items in the checkbox are taken as an array. Then in your php script, you do this;

if(empty($_REQUEST['item'])) {
    // No items checked
}
else {
    foreach($_REQUEST['item'] as $id) {
        // delete the item with the id $id
    }
}

yes this is also the best way to create an array of the checkbox. @davy_yg did you get this?

Does the loop knows which items that I checked ?

Does it mean that the items that I checked passed an id number ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.