I want to insert the selected data into my database but I am keep getting an error message saying:
"Undefined index: checkbox".

<?php
$con=mysqli_connect("localhost","root","","sgptt");//database connection
[B]$checkbox=$_POST['checkbox'];[/B]
if($_POST["submit"]== "submit"){
    for ($i=0; $i<sizeof($checkbox); $i++){
            $order = "INSERT INTO studentlist
            (name,username)
            VALUES
            ('$_POST[name]','".$checkbox[$i]."')";

$result = mysqli_query($con,"SELECT * FROM studentlist");  //order execute
if(!mysqli_query($con,$order)){
    die('Error:'. mysqli_error($con));
}
header('Location:addStudent.php?Message=Group Successfully Added');
exit();
}
}
//echo $_POST["GroupName"];  
?>

    Im not sure what you are trying to achieve overall, looks a bit weird you are doing a for loop from a single checkbox, and using sizeof to determine how many iterations. Can you show the HTML of the form ?

    However for a simple fix you can do this.

    $checkbox = isset($_POST['checkbox']) ? $_POST['checkbox'] : '';
    

    You may need to change the empty string to better match what you are trying to do but at least the undefined index issue should be resolved.

      planetsim;11038609 wrote:

      Im not sure what you are trying to achieve overall, looks a bit weird you are doing a for loop from a single checkbox, and using sizeof to determine how many iterations. Can you show the HTML of the form ?

      However for a simple fix you can do this.

      $checkbox = isset($_POST['checkbox']) ? $_POST['checkbox'] : '';
      

      You may need to change the empty string to better match what you are trying to do but at least the undefined index issue should be resolved.

      Thank you for your reply. I want to search a student record either by their username or firstname or surname, and the result will return with a checkbox. Then I want to add the selected student to a list. This is what I have in my HTML page:

              <form action="addStudent.php" method="post">
                  <br><input id="search" name="find" type="text" placeholder="Username or Firstname or Surname"/><br>
              <input id="searchb" type="submit" value="Search"/><br></form>
              <fieldset id="searchStu" style="overflow-y: scroll;">
      
        <?php
      $con=mysqli_connect("localhost","root","","sgptt");//database connection
      
      if(isset($_POST['find'])){
      if(empty($_POST['find'])){
      $nameErr = "Missing";}
      else{
      $find = $_POST['find']; 
      
      $sql = mysqli_query($con,"SELECT * FROM useraccount WHERE username LIKE '%$find%' OR firstname LIKE '%$find%' OR surname LIKE '%$find%' ");
        echo "<table>
                          <tr>
                          <th>Username</th>
                          <th>First Name</th>
                          <th>Surname</th>
                          </tr>";
      while ($row = mysqli_fetch_array($sql)){
          echo "<form>";
          echo "<form action='agname.php' method='post'>";
          echo "<tr>";
          echo "<td>" .$row['username']."</td>";
          echo "<td>".$row['firstname']."</td>";
          echo "<td>".$row['surname']."</td>";
          echo '<td><input name="check" type="checkbox"></td>';
          echo "<tr/>";
          echo "</form>";
          }
       echo"</table>";
      }
       }
      mysqli_close($con); 
      ?>

        I want to add the selected student to a list by click on this submit button:

        <form action="agname.php" method="post">
                    <br><input id="lname" name="name" type="text" placeholder="New List Name"/>
                    <input id="add" name="submit" type="submit" value="Save"/><br></form>
          Write a Reply...