hello, i'm having trouble getting my delete selected checkbox working for a code I'm writing:
the trouble is really just in the bottom half of the code after i make the while loop...i made this off of an example that i found on a site but like it was pretty clear that the site's code had many errors for example...the coder made an input tag and later called it as a $variable which i'm pretty sure isn't possible..anyways some help would be much appreciated :)

<?php
//create a database connection
mysql_connect("localhost", "root","") or die(mysql_error());
//select your database
mysql_select_db("group1");
?>

<html>
<body background="schoolbackground.jpg">
<div style="border-bottom-style: solid; border-bottom-width: 1px; padding-bottom: 1px">
    <font color="#357EC7" size="7" face="century gothic">Student Delinquent List</font></div>
 <p>&nbsp;</p>
<a href="textbook_teachhome.htm">
<p align="right">
<button type="button"><font face="century gothic" size="2" color="#357EC7">Go Back</font></button></a>
</p>
<p align="center">


<?php

$query = mysql_query("SELECT * from r_textbook_info");
$count = mysql_num_rows($query);
?>

<table frame="border" rules="all" cellpadding="5" bgcolor="#FFFFFF">  
<tr>
<th></th>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">      
<th>Student Number</th>
<th>Course</th>
<th>Year</th>      
<th>Book ID</th>
<th>Book Name</th>
<th>Cost</th>
</tr>

<?php
//return the array
while ($row = mysql_fetch_array($query)){

    $sn = $row['student_number'];
    $course = $row['course'];
    $year = $row['year'];
    $bid = $row['book_id'];
    $bname = $row['book_name'];
    $cost = $row['cost'];
?>
<tr>
 <th><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $bid; ?>"/></th>
 <th><? echo $sn; ?></th>
 <th><? echo $course; ?></th>
 <th><? echo $year; ?></th>
 <th><? echo $bid; ?></th>
 <th><? echo $bname; ?></th>
 <th><? echo $cost; ?></th>
</tr>

<?php
}
?>

</table>


<?php
// Check if delete button active, start this 
if(isset($_POST['delete'])){
 for($i=0;$i<$count;$i++){
    if(isset($_POST['checkbox[]']))
 {$del_id = $bid[$i];
 echo $del_id;
 $sql = "DELETE FROM r_textbook_info WHERE id='$del_id'";
 $result = mysql_query($sql);}
 }

// if successful redirect to delete_multiple.php 
if($result){
 echo "<meta http-equiv=\"refresh\" content=\"0;URL=teach_delinquent.php\">";
 echo "SUCCESS";
 }
 }
 mysql_close();
 ?>
<input name="delete" type="submit" id="delete" value="Delete"/>
</p>
</form>
</html>

Recommended Answers

All 14 Replies

Hello,
If you temporary comment the delete block of code and add an print_r($_POST) you can read in which format you get the data, something like this:

Array
(
    [checkbox] => Array
        (
            [0] => 3
            [1] => 8
            [2] => 17
        )

    [delete] => Delete
)

if you select an element to delete, otherwise you get:

Array
(
    [delete] => Delete
)

so, in the delete block instead of using $count which is related to the query result, you can count() the checkbox array and do a loop or use implode() to run just one query, like this:

if(isset($_POST['delete']))
{
    if(count($_POST['checkbox']) != 0)
    {
        $ids = implode(',', $_POST['checkbox']);
        $result = mysql_query("DELETE FROM r_textbook_info WHERE id IN($ids)") or die(mysql_error());
    }
 }

hope is useful, bye!

commented: this post saved me :) +0

hey cereal, thank you for the reply.
um i replaced the code in the delete block with 'if(isset($_POST['delete'])) etc.'
but now i'm getting an 'invalid arguments passed' for the implode function

In my code replace previous $_POST['checkbox'] with $_POST['checkbox[]']. I just forgot, sorry!

yeah i noticed that :)
but that isn't the problem

    $ids = implode(',', $_POST['checkbox']);

apparently contains invalid arguments
i've never used the implode function but i looked it up and i found it's usually (" ", $anarray)? excuse my ignorance...
was i supposed to define an array somewhere?

hey i changed by code a little...now it works :')

if(isset($_POST['delete']))
{
    if(count($_POST['checkbox']) !=0)
    {
        $array = array("checkbox" => $_POST['checkbox']);
        $ids = implode(',', $array);
        $result = mysql_query("DELETE FROM r_textbook_info WHERE id IN($ids)") or die(mysql_error());
    }
}

thanks cereal, i couldn't have figured it out without you

any time you use this syntax in a form
name="checkbox[]"
the post value in php is an array
so $_POST['checkbox'] is an array already

@choconom you're welcome

implode(), in this case, is used to separate each value of the array by a comma, so you get a string: 3,8,17 (ref. to previous post) instead of something to loop like array(3,8,17)

if you change $_POST['checkbox[]'] you can get rid of the line $array = array("checkbox" => $_POST['checkbox']); since you have already an array, as already stated by jstfsklh211

bye!

thanks to both of you guys for explaining that...but the reason i'm not using

checkbox[]

is because i get an error that it is undefined...my program works fine if its just 'checkbox'
i think it might be because you have to give it an index?
but new problem....i'm not able to delete multiple selected rows

Change your implode() with: implode(',', $array['checkbox']);
or there is another problem?

commented: super helpful post!! +0
commented: helpful. thanks +0

its says implode has invalid arguments
thanks for all the help :x

Please, post your updated code, it will be easier to find the problem :)

<?php
//create a database connection
mysql_connect("localhost", "root","") or die(mysql_error());
//select your database
mysql_select_db("group1");
?>

<html>
<body background="schoolbackground.jpg">
<div style="border-bottom-style: solid; border-bottom-width: 1px; padding-bottom: 1px">
    <font color="#357EC7" size="7" face="century gothic">Student Delinquent List</font></div>
 <p> </p>
<a href="textbook_teachhome.htm">
<p align="right">
<button type="button"><font face="century gothic" size="2" color="#357EC7">Go Back</font></button></a>
</p>
<p align="center">


<?php

$query = mysql_query("SELECT * from r_textbook_info");
$count = mysql_num_rows($query);
?>

<table frame="border" rules="all" cellpadding="5" bgcolor="#FFFFFF">  
<tr>
<th></th>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">      
<th>Student Number</th>
<th>Course</th>
<th>Year</th>      
<th>Book ID</th>
<th>Book Name</th>
<th>Cost</th>
</tr>

<?php
//return the array
while ($row = mysql_fetch_array($query, MYSQL_ASSOC)){

    $sn = $row['student_number'];
    $course = $row['course'];
    $year = $row['year'];
    $bid = $row['book_id'];
    $bname = $row['book_name'];
    $cost = $row['cost'];
    $id = $row['id'];
?>
<tr>
 <th><input name="checkbox" type="checkbox" id="checkbox[]" value="<?php echo $id; ?>"/></th>
 <th><?php echo $sn; ?></th>
 <th><?php echo $course; ?></th>
 <th><?php echo $year; ?></th>
 <th><?php echo $bid; ?></th>
 <th><?php echo $bname; ?></th>
 <th><?php echo $cost; ?></th>
</tr>

<?php
}
?>

</table>
<input name="delete" type="submit" id="delete" value="Delete"/>

<?php



// Check if delete button active, start this 
if(isset($_POST['delete']))
{
    if(count($_POST['checkbox']) !=0)
    {
        $array = array("checkbox" => $_POST['checkbox']);
        $ids = implode(',', $array['checkbox']);
        $result = mysql_query("DELETE FROM r_textbook_info WHERE id IN($ids)") or die(mysql_error());
    }

//if successful redirect to teach_delinquentlist.php 
if($result){
 echo "<meta http-equiv=\"refresh\" content=\"0;URL=teach_delinquentlist.php\">";
 }
 }
 mysql_close();
 ?>

</p>
</form>
</html>

ok i got thanks cereal THANK YOU SO MUCHHHH
i just needed to change input name from combobox to combobox[]

Hi!
I am trying to do the samethif (deleting row of selected checkbox) and i don't know what to change to make it work. Could someone help me please? :)

Thanks! Here is the code

P.S: i have tried to code in the same way as chcoconom did, but still didn't work.

    <body bgcolor="#FFF8DC">

    <!-- Irelevant lines -->

    <table align="center">
                    <tr>
                        <td>
                            <h1><font color="#191970">Jurnalul de Recrutare</font></h1> 
                        </td>
                    </tr>
                </table>

                            <table width="960px" border="0" cellspacing="1" cellpadding="3" align="center">
                                <tr bgcolor="#FFE4B5">
                                    <td align="right" valign="top">
                                    <strong>Cauta in jurnal</strong>
                                    </td>
                                    <td valign="middle">
                                    <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
                                        <select name="_searchC">
                                        <option value=""></option>
                                        <option value="idangajator">Angajator</option>
                                        <option value="idcandidat">Candidat</option>
                                        <option value="idpost">Post</option>
                                        <option value="data">Data</option>
                                        </select>
                                            <input type="text" name="_search" />
                                            <input type="submit" name="submit" value="Cauta" />
                                    </form>
                                    </td>
                                    <td valign="top">
                                        <strong>Ordoneaza dupa</strong>
                                        <select ONCHANGE="location = this.options[this.selectedIndex].value;">
                                            <option value=""></option>
                                            <option value="jurnal.php?_order1=idangajator">Angajator</option>
                                            <option value="jurnal.php?_order1=idcandidat">Candidat</option>
                                            <option value="jurnal.php?_order1=idpost">Post</option>
                                            <option value="jurnal.php?_order1=rezultat">Rezultat</option>
                                            <option value="jurnal.php?_order1=data">Data</option>
                                            <option value="jurnal.php?_order1=ora">Ora</option>
                                        </select>

                                    </td>
                                    <td align="left" valign="middle">
                                    <form action="add_jurnal_form.php" method="post">
                                        <input type="submit" name="adauga" value="Adauga" />
                                    </form>
                                    </td>
                                </tr>



                            </table>


                            <table width="960px" border="0" cellspacing="1" cellpadding="3" align="center">
                                <tr bgcolor="#A9A9A9">
                                    <td></td>
                                    <td align="center"><strong>Data</strong></td>
                                    <td align="center"><strong>Ora</strong></td>
                                    <td align="center"><strong>Candidat</strong></td>
                                    <td align="center"><strong>Angajator</strong></td>
                                    <td align="center"><strong>Post</strong></td>
                                    <td align="center"><strong>Rezultat</strong></td>
                                    <td align="center"><strong>Editeaza</strong></td>
                                    <td align="center"><strong>Sterge</strong></td>
                                </tr>


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

                                <tr bgcolor="#E6E6FA">
                                    <td>
                                        <input type="checkbox" name="check[]" id="check[]" value="<?php echo $rows['id']; ?>" />
                                    </td>
                                    <td><?php echo $rows['data']; ?></td>
                                    <td><?php echo $rows['ora']; ?></td>
                                    <td>
                                    <?php 
                                        $idcandidat=$rows['idcandidat'];
                                        include "conectarebd.php";
                                        $query1 = "SELECT nume FROM candidat where id like '$idcandidat'";
                                        $res1=mysql_query($query1);
                                        while($row=mysql_fetch_array($res1)){
                                        echo $row['nume'];}                                 
                                    ?>
                                    </td>
                                    <td>
                                    <?php 
                                        $idangajator=$rows['idangajator'];
                                        include "conectarebd.php";
                                        $query1 = "SELECT nume FROM angajator where id like '$idangajator'";
                                        $res1=mysql_query($query1);
                                        while($row=mysql_fetch_array($res1)){
                                        echo $row['nume'];}                                 
                                    ?>
                                    </td>
                                    <td>
                                    <?php 
                                        $idpost=$rows['idpost'];
                                        include "conectarebd.php";
                                        $query1 = "SELECT nume FROM post where id like '$idpost'";
                                        $res1=mysql_query($query1);
                                        while($row=mysql_fetch_array($res1)){
                                        echo $row['nume'];}                                 
                                    ?>
                                    </td>
                                    <td><?php echo $rows['rezultat']; ?></td>
                                    <td align="center"><a href="ed_jurnal_form.php?id=<?php echo $rows['id']; ?>">Editeaza</a></td>
                                    <td align="center"><a href="del_jurnal.php?id=<?php echo $rows['id']; ?>">Sterge</a></td>



                                </tr>


                                <?php
                                }
                                ?>
                                <tr>
                                    <td>
                                        <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" >
                                            <input type="submit" name="chdel" id="chdel" value="Sterge" />
                                        </form>
                                    </td>
                                </tr>

                                <?php
                                    if(isset($_POST['chdel']))
                                {

                                    if(count($_POST['check']) !=0)
                                    {

                                        $array = array("check" => $_POST['check']);
                                        $ids = implode(',', $array['check']);
                                        $result = mysql_query("DELETE FROM agenda_recrutare WHERE id IN($ids)") or die(mysql_error());
                                    }
                                }

                                ?>

                            </table>

        <!-- Irelevant lines -->
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.