Please Help!! I have created a mysql database then checked for duplicates, but i got some problem with array sent through a checkbox. I am trying to use foreach to call each array.

here is the form:

echo '<tr><td>'.'<input type="checkbox" name="id[]" value="'.$row['id'].'"/></td>'. // other code continues here.....

it is from mysql table named 'backup' that have 75 duplicates.

and here is my code to "catch" the the array from the checkboxes.

$delete = $POST['delete'];
$array_id = $
POST['id'];
if (isset($delete) && is_array($array_id))
{
foreach ($array_id as $id)
{
echo "ID:".$array_id." %ID: ".$id."<br>";
mysql_query("DELETE FROM backup WHERE ID='$id'")or die(mysql_error());
}

	}
    else
    {
	    echo "An error occurred during deletion process!<br />\n";
    }

Thanks in advance!

    Sorry if wasn't so clear...
    I was trying to echo the array from "$_POST['id']" with the variable $array_id to check the output like this : echo "ID:".$array_id."<br>";

    and it give me : ID:Array

    i thought the $array_id is already an array but why can't i get the it with foreach()😕
    I haven't used this function before and i dont how to fix it.:o

      jarriola wrote:

      i thought the $array_id is already an array

      Yes, it is an array.. just as PHP told you. [man]foreach/man loops through the array, placing each array item in the variable $id (in your code, anyway).

        But why can't the mysql_query("DELETE FROM backup WHERE ID='$id'") doesnt seem to work. did i code it right??😕

          How does it not work? For error checking, you can write:

          echo "DELETE FROM backup WHERE ID='$id'";
          mysql_query("DELETE FROM backup WHERE ID='$id'") OR die(mysql_error());

            Thanks for quick reply!!
            I already tried to echo the $id several times, still it doesn't gives the $id. but when i tried to echo the mysql_query like this:

            $del = mysql_query("DELETE FROM backup WHERE ID='$id'");
            echo $del;

            it gives me: 1

            and when i selected 3 checkboxes it gave : 111.

            what's happening? what does that mean?

              $del is the return value of mysql_query(). What you want to print is $id.

                right!, i dont know if i did it right, but i tried to echo the mysql_query to know if the statement is true or not (1 or 0) so i could "guess" if the mysql_query is running as expected. but it did not though. 🙂
                I do not know what's wrong with it.

                  right!, i dont know if i did it right, but i tried to echo the mysql_query to know if the statement is true or not (1 or 0) so i could "guess" if the mysql_query is running as expected. but it did not though.

                  That is the reason for my suggestion of:

                  mysql_query("DELETE FROM backup WHERE ID='$id'") OR die(mysql_error());

                  If mysql_query() returns 0 (a value that evaluates to false), the script will then exit and print a more detailed error message from mysql_error().

                    mysql_query("DELETE FROM backup WHERE ID='$id'") OR die(mysql_error());

                    im really new to PHP & MYSQL but i hope you've noticed this in my code, i do use this code too but what i want to know is how am i gonna get the value of $id from an array so i can use it with that mysql_query. I'm still trying to find out. anyway you did a great job!! 🙂

                      As far as I can tell, you are using the foreach loop correctly.

                      To be more pedantically correct:

                      if (isset($_POST['delete'], $_POST['id']) && is_array($_POST['id']))
                      {
                          foreach ($_POST['id'] as $id)
                          {
                              $id = (int)$id;
                              mysql_query("DELETE FROM backup WHERE ID=$id") or die(mysql_error());
                          }
                      }
                      else
                      {
                          echo "An error occurred during deletion process!<br />\n";
                      }

                      EDIT:
                      Of course, an even better way would be to execute just one SQL statement:

                      if (isset($_POST['delete'], $_POST['id']) && is_array($_POST['id']))
                      {
                          $ids = array();
                          foreach ($_POST['id'] as $id)
                          {
                              $ids[] = (int)$id;
                          }
                      
                      if (count($ids) > 0)
                      {
                          $ids = implode(',', $ids);
                          mysql_query("DELETE FROM backup WHERE ID IN ($ids)")
                              or die(mysql_error()); // for debugging
                      }
                      }
                      else
                      {
                          echo "An error occurred during deletion process!<br />\n";
                      }

                        hello laserlight, I tried to copy-Paste your code, but still it's not working, im thinking if it has something to do with MySQL & PHP version i'm using
                        (PHP Version 5.2.5 - MySQL 5.0.51).
                        But i tried to check errors with "error_reporting(E_ALL)" and it gave me this:
                        Notice: Undefined index: id in G:\xampp\htdocs\CDataQ\check_duplicate.php on line 82

                        Sorry, I really don't know what's happening with it,
                        i understand the foreach() i think but not in this case.:o

                          But i tried to check errors with "error_reporting(E_ALL)" and it gave me this:
                          Notice: Undefined index: id in G:\xampp\htdocs\CDataQ\check_duplicate.php on line 82

                          Ah, then the problem is not with PHP or MySQL, but with your code. My example code does not suffer from that problem since it checks with isset that incoming variables like $_POST['id'] exist before using them.

                            Maybe i should try to restructure the code, but i'll keep your sample codes, it will help
                            me a lot! thanks!!! i appreciate your effort.
                            i'll try to inform you when i get it, may be soon. 🙂

                              Write a Reply...