Hi all, this is really geting to me now, I jsut keep going round in circles and getting nowhere.

I have a form with multiple radio buttons, each button represents a category of reptile. When you choose one of the radio buttons you are taken to a page for that category which displays all the entries for that category that have been stored in a mysql table with a checkbox beside each entry. When I check one and hit the 'Remove' button it always removes the oldest entry in the table and not the one corresponding to the check box that was checked.
I need it to remoe all the checked entries, so say, if 6 were checked then that 6 will be removed from the table.

I really need help on this if anyone has the time.

Cheers.

My remove.php file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Stocklist update</title>
<link href="../css/gallery_submit.css" rel="stylesheet" type="text/css" />
<link href="../css/removestock.css" rel="stylesheet" type="text/css" />
<link href="../css/template.css" rel="stylesheet" type="text/css" />
<body>

<?php
      include 'connect.php';

      if ($_SERVER['REQUEST_METHOD'] == 'POST')
      {
         if (isset($_POST['next'])) 
         {
            $selected_radio = $_POST['delete'];
            echo '<h2><b>Boids</b></h2>';
    ?>
       <form class='form' action='remove2.php' method='post' enctype='multipart/form-data' name='stockupdate_form' id='stockupdate_form' style='margin-bottom:0px;'>;
          <?php
            if ($selected_radio == 'Boids') 
            {
                $data = mysql_query("SELECT * FROM boids  ORDER BY id DESC")
                       or die(mysql_error());

                       while($info = mysql_fetch_array( $data )) 
                       { 
                          echo "<span class='span2'>";
                          echo $info['species'];
                          echo ' : ';
                          echo $info['year'];
                          echo ' : ';
                          echo $info['sex'];
                          echo ' : ';
                          echo '&pound';
                          echo $info['price'];
                          echo "<input type='checkbox' name='check[]' />";
                          echo '<br />';
                          echo "</span>";

                       }
                 }

              }
        }
?>
   <input type='submit' name='remove' value='Remove' />
   </form>

</body>
</html>

My remove2.php file

<?php
      include 'connect.php';

        if ( isset($_POST['remove']))
       {

                 $data = mysql_query("SELECT * FROM boids")
                       or die(mysql_error());
         $info = mysql_fetch_array( $data );
         $id=$info['id'];
         $sql =  mysql_query("DELETE FROM boids WHERE id = $id");   

         while($info = mysql_fetch_array( $data )) 
         { 
            echo "<span class='span2'>";
            echo $info['species'];
            echo ' : ';
            echo $info['year'];
            echo ' : ';
            echo $info['sex'];
            echo ' : ';
            echo '&pound';
            echo $info['price'];
            echo '<br />';
            echo "</span>";

                       }
            }
?>

Thanks for looking.................

Member Avatar for LastMitch

@GlenRogers

When I check one and hit the 'Remove' button it always removes the oldest entry in the table and not the one corresponding to the check box that was checked.
I need it to remoe all the checked entries, so say, if 6 were checked then that 6 will be removed from the table.

When I look at your code. I notice your <form> is wrong.

Here is a couple of linkes about checkboxes deleting data from mysql:

http://fixbug.in/article-zone/item/6-delete-all-checked-checkbox-data--php-mysql

http://www.phpeasystep.com/mysql/8.html

can you follow the same format. I know you have different variables but same query but you can still used the same format.

Member Avatar for diafol
echo "<input type='checkbox' name='check[]' />";

If you have the actual id of the netry in the name, you can get the id into the action page, e.g.

echo "<input type='checkbox' name='check[<?php echo $info['id'];?>]' />";

You try to access $info['id'] in the action page (remove2.php), but it doesn't exist. These variables can't be passed from one page to the next due to the web's stateless nature. The only way to pass variables is via certain globals, such as through a form ($_POST or $_GET), a querystring ($_GET), via cookie ($_COOKIES) or session ($_SESSION). You don't need anything other than the rectified code I provided.

You pick up the ids of all the animals you want to remove like this in remove2.php:

if(isset($_POST['check'])){
    $id_string = implode(",",$_POST['check']);
    $sql = "DELETE FROM table WHERE id IN ($id_string)";
    ...
}

Note - you need to validate $_POST['check'] as a form could be spoofed and rubbish sent to your server. There are many cleaning functions you can use, and for an array, you can do soemthing like this:

$post = (array) $_POST['check'];
$clean_checks = array_map("intval",$post);

You could use mysql_real_escape_string() instead of intval().

As you're using this as an admin user, you probably think you don't need this, but I'd do it anyway and secure any delete procedure with a session variable looking for admin level rights. SOme may even go further with fixing a token to their forms to stop CSRF.

Thanks for the replies guys.

diafol. i am getting this error ' Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\wamp\www\new_reptile2\php\remove.php on line 39'

for this line

echo "<input type='checkbox' name='check[<?php echo $info['id'];?>]' />";

Any idea why this is happening?

Thanks

Member Avatar for diafol

AH silly me, forgot it was in php echo:

echo "<input type='checkbox' name='check[{$info['id']}]' />";

I seem to have a problem, now it deletes nothing from my table!

this is my code now.

remove.php

<?php
      include 'connect.php';

      if ($_SERVER['REQUEST_METHOD'] == 'POST')
      {
         if (isset($_POST['next'])) 
         {
            $selected_radio = $_POST['delete'];
            echo '<h2><b>Boids</b></h2>';
    ?>
       <form class='form' action='remove2.php' method='post' enctype='multipart/form-data' name='stockupdate_form' id='stockupdate_form' style='margin-bottom:0px;'>;
          <?php
            if ($selected_radio == 'Boids') 
            {
                $data = mysql_query("SELECT * FROM boids  ORDER BY id DESC")
                       or die(mysql_error());

                       while($info = mysql_fetch_array( $data )) 
                       { 
                          echo "<span class='span2'>";
                          echo $info['species'];
                          echo ' : ';
                          echo $info['year'];
                          echo ' : ';
                          echo $info['sex'];
                          echo ' : ';
                          echo '&pound';
                          echo $info['price'];
                          echo "<input type='checkbox' name='check[{$info['id']}]' />";
                          echo '<br />';
                          echo "</span>";

                       }
            }

         }
      }
      ?>
   <input type='submit' name='remove' value='Remove' />
   </form>

remove2.php

<?php
      include 'connect.php';

      if(isset($_POST['check']))
       {
          $id_string = implode(",",$_POST['check']);
          $sql = "DELETE FROM boids WHERE id IN ($id_string)";

?>

Thanks for looking!

Member Avatar for diafol

I'm losing my marbles. 1000 apologies - I forgot the array keys. Here's an example. It's a standalone so you can use it as it is to check it out:

<?php
if(isset($_POST['check'])){
    $chk = (array) $_POST['check'];
    $p = implode(',',array_keys($chk)); 
    echo $p;
}
?>

<form method="post">
    <input type="checkbox" name="check[1]" />
    <input type="checkbox" name="check[2]" />
    <input type="checkbox" name="check[3]" />
    <input type="checkbox" name="check[14]" />
    <input type="submit" value = "send" />
</form>

or you could even use the values attribute:

<?php
if(isset($_POST['check'])){
    $chk = (array) $_POST['check'];
    $p = implode(',',$chk); 
    echo $p;
}
?>
<form method="post">

<input type="checkbox" name="check[]" value="1" />
<input type="checkbox" name="check[]" value="2" />
<input type="checkbox" name="check[]" value="3" />
<input type="checkbox" name="check[]" value="14" />
<input type="submit" value = "send" />

</form>
commented: Kindness is a virtue ! +6

Thats it sorted now! Thanks for your help, I really appreciate it...................

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.