dream8877 2016-05-12 06:09
浏览 46
已采纳

填充单选按钮并向查询数据库提交值

I am trying to learn programming and have to populate a set of radio buttons and submit what is selected to show records from a database. I have already done this with a selection list, but can't quite understand what I need to change to convert it to a radio buttons.

Selection list:

<?php 
require_once("dbconn.php");
$sql = "SELECT staffName, staffID FROM staff";  
$rs = mysqli_query($dbConn, $sql) or die ('Problem with query' . mysqli_error($dbConn));
?>

<form id="task9" action="task7.php" method="get">   
    <select name="staffID" id="staffID">
    <?php
    while($row = mysqli_fetch_array($rs)) { 
        $name=$row["staffName"];
        $staffIden=$row["staffID"];
        echo "<option value=".$staffIden.">".$name."</option>";
    }
    ?>  
    <br><br>
    <input type="submit" name="submit" method="get">
    <input type="reset"  name="reset">
</form>

Radio buttons (all I get is all the names and only One radio button):

<?php 
require_once("dbconn.php");
$sql = "SELECT staffName, staffID FROM staff";  
$rs = mysqli_query($dbConn, $sql) or die ('Problem with query' . mysqli_error($dbConn));
?>

<form id="task9" action="task7.php" method="get">   
    <input type = "radio" name="staffID" id="staffID">
    <?php
    while($row = mysqli_fetch_array($rs)) { 
        $name=$row["staffName"];
        $staffIden=$row["staffID"];
        echo "<option value=".$staffIden.">".$name."</option>";
    }
    ?>  

Hopefully this question is clear enough.

  • 写回答

3条回答 默认 最新

  • doucu5836 2016-05-12 06:19
    关注

    Try using below code.

    <?php 
       require_once("dbconn.php");
       $sql = "SELECT staffName, staffID FROM staff";  
       $rs = mysqli_query($dbConn, $sql)
          or die ('Problem with query' . mysqli_error($dbConn));
    ?>
    
    <form id="task9" action="task7.php" method="get">   
    
    <?php
     while($row = mysqli_fetch_array($rs)) { 
     $name=$row["staffName"];
     $staffIden=$row["staffID"];
     echo "<label>";
     echo "<input type='radio' name='staffID' value='".$staffIden."'/> ";
     echo $name;
     echo "</label><br/>";
     }
     ?> 
    
     <br><br>
    
    <input type="submit" name="submit" method="get">
    <input type="reset"  name="reset">
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀