使用PHP,HTML和MySQli动态创建复选框列表

时间:2016-05-05 04:29:37

标签: php html mysqli

我正在尝试使用我的数据库中的员工动态创建一个复选框列表。该清单是为了参加会议。我尝试使用我在此站点上找到的解决方案来创建列表,但我收到的错误是我的代码中出现意外结束。我找不到了。

<?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>
               }

           }

3 个答案:

答案 0 :(得分:0)

您需要正确终止PHP代码:

....
</form>
</div>
<?
    }
}
?>

答案 1 :(得分:0)

在代码的末尾,您需要一个额外的PHP标记用于结束},它当前打印为HTML

答案 2 :(得分:0)

实际上还没有完成你的while循环和括号语法错误。您的if条件代码替换为以下代码。

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
               }

           }
           ?>