Dusty Hawkins Dusty Hawkins - 7 months ago 28
HTML Question

Dynamically create a checkbox list with PHP, HTML and MySQli

I'm trying to dynamically create a checkbox list using employees from my database. The list is for the purpose of taking attendance at meetings. I tried using a solution I found on this site to create the list but I'm getting an error that there is an unexpected end in my code. I can't find it.

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "purpletrainer";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";

$MeetingID = $_POST["meetingid"];

$sql = "SELECT employee.emplname
FROM purpletrainer.employee, purpletrainer.meeting
WHERE employee.empmngrid=meeting.meetingleader
AND meetingid='$MeetingID'";

$result = $conn->query($sql);

//Iterate over the results that you've gotten from the database
if ($result->num_rows > 0){
while($employee = $result->fetch_assoc())
{
?>
<div>
<form action="processlist.php" method="post">
<span><?php echo $employee['emplname']; ?></span>
<input type="checkbox" name="employees[]" value= '<?php echo $employee[0]; ?>' /><br />
</form>
</div>
}

}

Answer

Actually have not complete your while loop and if bracket syntax error. Your if condition code replace with below code.

if ($result->num_rows > 0){
               while($employee = $result->fetch_assoc())
               {
               ?>
                       <div>
                               <form action="processlist.php" method="post">
                               <span><?php echo $employee['emplname']; ?></span>
                               <input type="checkbox" name="employees[]" value= '<?php echo $employee[0]; ?>' /><br />
                               </form>
                       </div>
            <?php
               }

           }
           ?>
Comments