Deleting from MySQL using checkbox

Hello,

As tittle says, I need to make a PHP/HTML page that will load the info from database table from MySQL and list the same info trough a HTML table but within the table there should be a check box where I can select a specific row and once that is done and press on button Delete, it should post the selected items on another PHP page and delete the rows/ID’s which i selected from database.

Now I’ve been using an array for getting all the selected items (check box-es) that I choose, and post them on the another page but i think i am doing something wrong in naming the value of check box or i don’t use an array properly as i keep getting an error “undefined offset 1”. Either way, is there some plain check and select delete way to do this, and if not what is the best way i could make it?

Thanks

Dushan

It’s a litle hard to help troubleshoot your code unless you post it :wink:

If you have an array of form elements like checkboxes, as in:

[php]

[/php]

Then in your code, the array keys would simply be referenced as in:

[php]
echo $_POST[‘cb’][0];
echo $_POST[‘cb’][1];
echo $_POST[‘cb’][2];

if (!empty($_POST[‘cb’][0])) {
// Do Something
}
[/php]

You can check the values/array keys with print_r() after the form is submitted:

[php]

print_r($_POST);

// Output:
/*
Array
(
[0] => on
[1] =>
[2] =>
[3] => on
)
*/

[/php]

If you need more specifics, post the relevant portion of code.

Thanks for your reply,

Sorry i didn’t provide a code at the time i asked for help my code was too messy but i’ll post now.

I have this table done like this:

    <table>
                <tr>
                    <th><b>Zemlja</b></th>
                    <th><b>Destinacija</b></th>
                    <th><b>Tip Putovanja</b></th>
                    <th><b>Tip Transporta</b></th>
                    <th><b>Datum polaska</b></th>
                    <th><b>Datum povratka</b></th>
                    <th><b>Cena</b></th>
                    <th><b>Obrisi</b></th>
                    <th><b>ID</b></th>
                </tr>

[php]

<?php while($rows = mysql_fetch_array($result)) { ?>

[/php]


<tr>
                    <td><?php echo $rows['Zemlja']; ?></td>
                    <td><?php echo $rows['Destinacija']; ?></td>
                    <td><?php echo $rows['Tip_Putovanja']; ?></td>
                    <td><?php echo $rows['Tip_transporta']; ?></td>
                    <td><?php echo $rows['Polazak']; ?></td>
                    <td><?php echo $rows['Povratak']; ?></td>
                    <td><?php echo $rows['Cena']; ?></td>
                    <td><input type="checkbox" name="checkbox[]" value="<?php echo $rows['ID'] ?>"></td> 
                    <td><?php echo $rows['ID']; ?></td>
                </tr>

[php]

<?php } ?>

[/php]

</table>

Now that row where is input for check box, i named it “checkbox[]” as it can be seen, but now i want that every check box have its value numbered by AUTO INCREMENT-ed ID from database table, and post it to another PHP page where i could delete all the selected check box-es that match the ID or value that it has been set to?

Right now my POST page have only one variable that goes like:

[php]
$selekt = $_POST[‘checkbox’];
[/php]

And whenever I tried to make it be an array I kept getting that error “undefined offset 1”. I’ll try to see and use ur code that u posted and i’ll come back later , hopefully, with some solution and post it here. Untill then i’d appriciate help again if this made my problem clearer.

Thanks

Dushan

haha and i just did it :smiley: :smiley: :smiley:

Thanks to your code mostly, I didn’t know that checkbox named with [] works as an array, when i realized that all i did was, checking if the checkbox is empty or not:

[php]
if(!empty ($_POST[‘checkbox’]))
{
$selekt = $_POST[‘checkbox’];
}
else
{
echo “”;
}
[/php]

And then just a foreach item of an array that is in the checkbox delete the ID like this:

[php]

if(!empty($selekt))
{
foreach($selekt as $s)
{

        $kveri = "DELETE FROM aranzman WHERE ID = '$s'";
        $result = mysql_query($kveri);
        
        echo "<h1>Uspesno ste obrisali izabrane aranzmane</h1>"; //Success message
       
    }
    }
    else
    {
        echo "<h1>Niste izabrali aranzmane za brisanje.</h1>"; //Fail message for not selecting anything
    }

[/php]

It works great now, Thanks again and i love this forum ! :o :smiley: :stuck_out_tongue: :slight_smile:

You’re welcome, I’m glad it’s working… 8)

Sponsor our Newsletter | Privacy Policy | Terms of Service