下拉列表php与数据库中的选定值

时间:2016-09-27 06:12:18

标签: php mysql

我一直在php的下拉框中工作,它链接到表profile_staff和potential_client表结构如下

表1 - potential_client

pc_id | pc_staff    1 | prav

表2 - profile_staff staff_id | staff_name     1 | SHREE     2 | prav

其中填充值应来自表profile_staff列staff_name,并且所选值应来自potential_client列staff_id。下面是我到目前为止所做的代码。它在下拉列表中显示空值。请指教。

<?php  
$sql2 = "SELECT profile_staff from potential_client WHERE pc_id = '$pc_id'";
$sql1= "SELECT staff_name from profile_staff";

$result1 = mysql_query($sql1);
$result2 = mysql_query($sql2);

echo '<select name="pc_staff">';
while($row = mysql_fetch_array($result1) && $row1 =      mysql_fetch_array($result2)) {
if ($row === $row1) {
echo '<option value="' . $row . '" selected="selected" />';
} else {
echo '<option value="' . $row1 . '" />';
}
}
echo '</select>';  ?>

1 个答案:

答案 0 :(得分:0)

谢谢你们这里的工作代码。

<?php
$sql = "SELECT staff_name FROM profile_staff";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

echo "<select name='pc_staff' <option value = '0'> Select Staff </option>";
while ($row = mysql_fetch_array($result)) {
if ($row['staff_name'] == $search_user['pc_staff']) {
    $selectCurrent=' selected';
echo '<option value="'.$search_user['pc_staff'].'"  '.$selectCurrent.'>'.$search_user['pc_staff'].'</option>';
}
else
 {
echo "<option value='" . $row['staff_name'] ."'>" . $row['staff_name'] ."  </option>";
  }
 }
 echo "</select>"; ?>