删除多行函数中的未定义变量和索引

Edit:I added this

if (isset($_POST['delete']) && isset($_POST['checkbox'])) (get from an answer below but it seems the asnwer is gone)

and now the undefined varialbe and index error are gone...but the problem now is when I try to delete , it require check and click delete button twice to delete the selected rows...

  <form name="frmSearch" method="post" action="insert-add.php">

<table width="600" border="1">
  <tr>
    <th width="50"> <div align="center">#</div></th>
    <th width="91"> <div align="center">ID </div></th>
    <th width="198"> <div align="center">First Name </div></th>
    <th width="198"> <div align="center">Last Name </div></th>
    <th width="250"> <div align="center">Mobile Company </div></th>
    <th width="100"> <div align="center">Cell </div></th>
    <th width="100"> <div align="center">Workphone </div></th>
    <th width="100"> <div align="center">Group </div></th>
  </tr>
    </form>
<?

    echo "<form name='form1' method='post' action=''>";

while($objResult = mysql_fetch_array($objQuery))
{


    echo "<tr>";
    echo "<td align='center'><input name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"$objResult[addedrec_ID]\"></td>";
    echo "<td>$objResult[addedrec_ID] </td>";
    echo "<td>$objResult[FirstName]</td>";
    echo "<td>$objResult[LastName] </td>";
    echo "<td>$objResult[MobileCompany] </td>";
    echo "<td>$objResult[Cell] </td>";
    echo "<td>$objResult[WorkPhone] </td>";
    echo "<td>$objResult[Custgroup] </td>";

   echo "</tr>";

}


 echo "<td colspan='7' align='center'><input name=\"delete\" type=\"submit\" id=\"delete\" value=\"Delete\">";

 if (isset($_POST['delete']) && isset($_POST['checkbox'])) // from button name="delete"
 {
 $checkbox = ($_POST['checkbox']); //from name="checkbox[]"
     $countCheck = count($_POST['checkbox']);

 for($d=0;$d<$countCheck;$d++)
     {
         $del_id  = $checkbox[$d];

 $sql = "DELETE from UserAddedRecord where addedrec_ID = $del_id";

 $result2=mysql_query($sql)  or trigger_error(mysql_error());;;

     }
         if($result2)
     {  
          $fgmembersite->GetSelfScript();
         }
         else
         {
 echo "Error: ".mysql_error();
         }
 }
  echo "</form>";

Thanks for every reply.

The first notice is due to no $_POST["checkbox"] variable. If you're using AJAX, inspect your query with firebug to check if you're sending a checkbox variable.

The second undefined is coming from your nesting. See my below example:

$foo = 0
if($foo ==1){
    $bar = 5;
}
if($bar == 2){
    //Blah
}

If you were to run that then $bar would never be set, hence the notice. As $countCheck is set to 0 as $_POST["checkbox"] is not set, then the for loop never runs hence $result2 is never set.

Your problem, you have a form nested inside another form.

  <form name="frmSearch" method="post" action="insert-add.php">

and in the middle you have

    echo "<form name='form1' method='post' action=''>";

Remove the 2nd one, the "echo"'ed one that is...

Unless you close the first form, you cant launch another one inside of it...