填充MySQL数据库中的选择字段表单数据并选择正确的值

时间:2014-09-15 20:53:21

标签: php html mysql sql

我已经有一个显示文本输入的数据库查询,但经过一小时的代码调整后,我仍然无法弄清楚如何正确显示选择字段及其正确选择的选项。选择的选项现在显示为一个额外的字段,这是不正确的方法。

我想显示帐户类型和信用类型的选定选项,这些选项也在用户表格中,但我不知道如何使用当前代码实现它。

感谢任何帮助。

    <?php $result = mysqli_query($db,"SELECT * FROM users");
echo "<table style='width:650px'>";
echo "<tr style='color:#CC0000'><td>ID</td><td>Username</td><td>Password</td><td>Phone</td><td>Email</td><td>Account Type</td><td>Credit Type</td><td>Credit</td></tr>";
    while($row = mysqli_fetch_array($result))
      {
      echo "<tr><td>" . $row['uid'] . "</td><td>" . $row['username'] . "</td><td>" . "<input id='password' type='password' name='password' placeholder='*****'  style='width:80px' />" . "</td><td><input id='phone' type='text' name='phone' value='".$row['phone']."' style='width:100px' /></td><td><input id='email' type='text' name='email' value='".$row['email']."' style='width:100px' /></td><td><select id='accounttype' name='accounttype' style='width:125px' /><option>".$row['accounttype']."</option><option>Agency</option><option>Admin</option><option>Super Admin</option></select></td><td><select id='credittype' name='credittype' style='width:125px' /><option>".$row['credittype']."</option><option>Flexible</option><option>Strict</option><option>Unlimited</option></select></td><td><input id='credit' type='text' name='credit' value='".$row['credit']."' style='width:50px' /></td></tr>";

      } 
       echo "</table>";
       ?>

2 个答案:

答案 0 :(得分:1)

选择正确选项的正确方法是<option selected>value</option>。循环遍历值并添加所选属性。

答案 1 :(得分:0)

我终于自己做了,也许不是最干净的代码,但它有效:

    <?php $result = mysqli_query($db,"SELECT * FROM users");
echo "<table style='width:650px;text-align: center'>";
echo "<tr style='color:#CC0000'><td>ID</td><td>Username</td><td>Password</td><td>Phone</td><td>Email</td><td>Account Type</td><td>Credit Type</td><td>Credit</td></tr>";
    while($row = mysqli_fetch_array($result))
      {
      echo "<tr><td>" . $row['uid'] . "</td><td>" . $row['username'] . "</td><td>" . "<input id='password' type='password' name='password' placeholder='*****'  style='width:80px' />" . "</td><td><input id='phone' type='text' name='phone' value='".$row['phone']."' style='width:95px' /></td><td><input id='email' type='text' name='email' value='".$row['email']."' style='width:100px' /></td><td><select id='accounttype' name='accounttype' style='width:115px' /><option"; if ($row['accounttype'] == 'Agency') echo " selected";echo ">Agency</option><option"; if ($row['accounttype'] == 'Admin') echo " selected";echo ">Admin</option><option"; if ($row['accounttype'] == 'Super Admin') echo " selected";echo ">Super Admin</option></select></td><td><select id='credittype' name='credittype' style='width:105px' /><option"; if ($row['credittype'] == 'Flexible') echo " selected";echo ">Flexible</option><option"; if ($row['credittype'] == 'Strict') echo " selected";echo ">Strict</option><option"; if ($row['credittype'] == 'Unlimited') echo " selected";echo ">Unlimited</option></select></td><td><input id='credit' type='text' name='credit' value='".$row['credit']."' style='width:50px' /></td></tr>";

      } 
       echo "</table>";
       ?>