Warning: mysql_fetch_array() stops the while loop

A

Anonymous

Guest
I create an array in a page using checkboxes like the following:

Code:
echo "<TD ALIGN=\"CENTER\"><input type='checkbox' name='delete_entry[$idmatincom]' value='delete_entry'>$idmatincom</TD>\n";

The idea is to delete those rows in a table where delete_entry[] is clicked.

I checked it out directly with MySql (version 3) and there were 3 rows with idmatincomp 39, 47 and 48.


Code:
  $query = "SELECT idmatincom FROM materials_in_competitions WHERE record_competition = '$record_competition'";
  echo $query;
  $result = mysql_query($query);
  if(!$result) error_message(sql_error());

while($query_data = mysql_fetch_array($result)) {
      $idmatincom = $query_data["idmatincom"];
      echo "<P>idmatincom= ".$idmatincom."<BR>delete_entry($idmatincom)= ".$delete_entry[$idmatincom]."<P>"; //This is in order to find out the bug
      if($delete_entry[$idmatincom]) {
         $query = "DELETE FROM materials_in_competitions WHERE idmatincom = '$idmatincom'";
         $result = mysql_query($query);
      }
}


When I don't check anything it echoes back that it correctly check all 3 rows and it deleted none since there is now value for $delete_entry[$idmatincom]

SELECT idmatincom FROM materials_in_competitions WHERE record_competition = '22'

idmatincom= 39
delete_entry(39)=

idmatincom= 47
delete_entry(47)=

idmatincom= 48
delete_entry(48)=


BUT! If I check delete_entry[$idmatincom] where idmatincom=47 it deletes it correctly but then it stops checking and produces an error. Here is the complete output



SELECT idmatincom FROM materials_in_competitions WHERE record_competition = '22'

idmatincom= 39
delete_entry(39)=

idmatincom= 47
delete_entry(47)= delete_entry

DELETE FROM materials_in_competitions WHERE idmatincom = '47'


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in competitions_all.htm on line 193

The idea is to be able to check and delete multiple rows in one click, not just a single row of course.

Thanks in advance to anyone who will take the effort to ckeck this out and maybe help me find the bug.
 
You are reassigning $result within the loop on line 12 you can do a delete query and not specify a result variable.. i.e. just delete $result = on line 12
Code:
   $query = "SELECT idmatincom FROM materials_in_competitions WHERE record_competition = '$record_competition'";
   echo $query;
   $result = mysql_query($query);
   if(!$result) error_message(sql_error());

 while($query_data = mysql_fetch_array($result)) {
       $idmatincom = $query_data["idmatincom"];
       echo "<P>idmatincom= ".$idmatincom."<BR>delete_entry($idmatincom)= ".$delete_entry[$idmatincom]."<P>"; //This is in order to find out the bug
       if($delete_entry[$idmatincom]) {
          $query = "DELETE FROM materials_in_competitions WHERE idmatincom = '$idmatincom'";
         mysql_query($query);
       }
 }
 
Back
Top