1

I'm trying to delete a row from table which is connected to database using checkbox (and it wouldn't matter if its just one row or multiple rows), but it is not working, as in nothing happens. It doesn't delete, no errors or warnings appear just a refresh.

php:

 <?php
    if(isset($_POST['del_event']))
    {
     if(isset($_POST['check']) && count($_POST['check']))
     {
      $array = array("check" => $_POST['check']);
      $id = implode(',', $array['check']);
      $result = mysql_query("DELETE FROM event WHERE event_id = '$id'") or die(mysql_error());
      }
    }
  ?>

html:

<form class="buttons" method="post" action="event.php">
<div class="button-wrap">
<input type="button" id="add_event" name="add_event" value="Add Event"/>
<input type="submit" id="del_event" name="del_event" value="Delete Event"/>
</div>  
 </form>
<tbody class="tbody-event-list-table2">
<?php
 require "connection.php";
  $check = mysql_query("SELECT * FROM event") or die(mysql_error());
 if(mysql_num_rows($check) > 0)
{
while($row = mysql_fetch_array($check))
{
     $id = $row['event_id'];
    $name = $row['event_name'];
    $start = $row['start'];
    $end = $row['end'];
    $venue = $row['event_venue'];
echo 
"<tr>
  <td><input type='checkbox' name='check' id='check' class='check' value='$id'/><a href=''        
 class='event-link'>$name</a></td><td>$start</td><td>$end</td><td>$venue</td>";
 echo "</tr>";
 }
}   
else
{
  echo 
  "<tr>
   <td colspan='4'>There Are No Events.</td>
  </tr>";
}
?>
</tbody>

It is if(isset($_POST['del_event'])) because 'del_event' is a name of a delete button which is disabled unless a checkbox is checked.

<script>
 var chkbox = $(".check"),
            button = $("#del_event");
       button.attr("disabled","disabled");
        chkbox.change(function(){
            if(this.checked){
                button.removeAttr("disabled");
            }else{
                button.attr("disabled","disabled");
            }
        });
  </script>

php version is 5.5.9

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Is that all your code? Which PHP version do you use? – damian Apr 25 '14 at 07:17
  • $_POST['check'] probably isn't set. Use something like: if (isset($_POST['check']) && count($_POST['check']) != 0) ... You should always post the full error message by the way. the error message you posted is missing the line number, for example. – Martin Tournoij Apr 25 '14 at 07:45
  • that is all the code for the specifics that im trying to do. –  Apr 25 '14 at 08:12

2 Answers2

0

This would still give you an undefined error if it is the check variable

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

but if a checkbox is off in a form it is not sent with the form so it does not exist. Carpetsmoker was halfway right, a proper solution would be:

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

    }
}

btw, it is better to always cast your variables to a propert type you are checking for. In PHP 0 can be 0, false or null.

Prevent this by safe type checking with the ===

klapvee
  • 139
  • 7
  • Nothing wrong with `&&`. If the first test (`isset`) fails, the second won't be executed. – Martin Tournoij Apr 25 '14 at 07:44
  • Excuse me apparently if you put it in that order it is ok, however if you would do this: if (count($_POST['check']) != 0 && isset($_POST['check']) ) it will give you an undefined variable error – klapvee Apr 25 '14 at 08:29
  • thanks for the reply. i tried both and it is still unable to delete. and no error/warnings appear –  Apr 25 '14 at 08:59
  • Your code says that if del_event is set than see if check exists. But maybe you can var_dump first if del_event actually comes through. I don't see the actual delete button in your code but i see that you have $("#del_event") , which means you address it by id. So is name="" also properly set? – klapvee Apr 25 '14 at 11:01
  • btw , isn't it $(this).checked instead of this? Im not a jQuery expert :) – klapvee Apr 25 '14 at 11:03
  • @klapvee im not a jquery expert myself. i learn as i go. so i googled this vs this and http://stackoverflow.com/questions/1051782/jquery-this-vs-this sums it up. –  Apr 28 '14 at 03:03
0

so i fixed the prob. first i had to remove "form" that was separating the buttons and the table so now "form" is wrapped around both buttons and table. second is the php coding for deletion has changed too.

<form class="buttons" method="post" action="event.php">
<div class="button-wrap">
<input type="button" id="add_event" name="add_event" value="Add Event"/>
<input type="submit" id="del_event" name="del_event" value="Delete Event"/>
<input type="submit" id="edit_event" name="edit_event" value="Edit Event">
</div>
<div class="event-list-table2" >
    <table class="event-table">
            <thead>
                <tr>
                    <th>Event Name</th>
                    <th>Start Event</th>
                    <th>End Event</th>
                    <th>Venue</th>
                </tr>
            </thead>
            <tbody class="tbody-event-list-table2">
                <?php
                    require "connection.php";
                    $check = mysql_query("SELECT * FROM event") or die(mysql_error());
                    if(mysql_num_rows($check) > 0)
                    {
                        while($row = mysql_fetch_array($check))
                        {
                            $id = $row['event_id'];
                            $name = $row['event_name'];
                            $start = $row['start'];
                            $end = $row['end'];
                            $venue = $row['event_venue'];
                            echo 
                            "<tr>
                                <td><input type='checkbox' name='check[]' class='check' value='$id'><a href='' class='event-link' value='$id' name='event-link'>$name</a></td><td>$start</td><td>$end</td><td>$venue</td>";
                            echo "</tr>";
                        }
                    }   
                    else
                        {
                            echo 
                            "<tr>
                                <td colspan='4'>There Are No Events.</td>
                            </tr>";
                        }
                ?>
            </tbody>
<?php
    if (isset($_POST['del_event']) && isset($_POST['check']))
    {
        foreach($_POST['check'] as $del_id)
        {
            $del_id = (int)$del_id;
            $sql = mysql_query("DELETE FROM event WHERE event_id = $del_id") or die(mysql_error());
                if($sql)
                {
             echo "<meta http-equiv=\"refresh\" content=\"0;URL=event.php\">";
}
        }
    }
?>
    </table>
</div>
     </form>