重复数据库中的SELECT下拉列表值

时间:2015-12-03 10:38:44

标签: php mysql database select

我正在使用PHP来更改数据库中下拉列表菜单的值。 我使用的状态是:新 - 进度 - 等待 - 完成 - 关闭 假设所有新条目都具有状态" NEW"

当我更改值时,我仍然可以看到双重选项。

这是我的代码:

<td>
    <select class='form-control col-sm-10' id='status' name='status'>
        <option value=" . $row['status'] . " >" . $row['status'] . "</option>
        <option value='new'>New</option>
        <option value='progress'>Progress</option>
        <option  value='wait'>Wait</option>
        <option value='done'>Done</option>
        <option value='close'>Close</option>
    </select>
</td>

图片:enter image description here

---------------------完全代码--------------

<?php

include("../includes/connection.php");

if ($link->connect_errno > 0) {
    die('Unable to connect to database [' . $link->connect_error . ']');
}

if (isset($_POST['update'])) {
    $results = $link->query("UPDATE job SET status='$_POST[status]', priority='$_POST[priority]' WHERE id='$_POST[hidden]'");
    $results = $link->query("UPDATE customer SET status='$_POST[status]' WHERE id='$_POST[hidden]'");
}

$sql = "SELECT * from job";
if (!$result = $link->query($sql)) {
    die('There was an error running the query [' . $link->error . ']');
}
echo "
<table class='table'>
    <thead>
        <tr>";
/* Get field information for all columns */
while ($finfo = $result->fetch_field()) {
    echo "
        <th>" . $finfo->name . "</th>";
}
echo "
        </tr>
    </thead>
    <tbody>";


while ($row = $result->fetch_assoc()) {

      $job_id = $row['id'];
    echo "<form action='' method=post>";

    echo "<tr class='info'>

                <input type=hidden name=hidden value=" . $row['id'] . ">
                <td>" . $row['id'] . "</td> 
                <td>" . $row['device'] . "</td>
                  <td>" . $row['model'] . "</td> 
                <td>" . $row['problem'] . "</td>

                <td><select class='form-control col-sm-10' id='status' name='status'>
                                  <option value=" . $row['status'] . " >" . $row['status'] . "</option>
                  <option value='new'  >New</option>
                  <option value='progress'>Progress</option>
                  <option  value='wait'>Wait</option>
                  <option value='done'>Done</option>
                  <option value='close'>Close</option>
                       </select></td>

                <td><select class='form-control col-sm-10' id='priority' name='priority'>
                        <option value=" . $row['priority'] . "  >" . $row['priority'] . "</option>
                        <option value='high'>High</option>
                        <option value='medium'>Medium</option>
                        <option  value='low'>Low</option>
                       </select></td>

                <td> <button type='submit' class='btn btn-primary btn-sm' name='update'>Update</button></td>

                <td> <a class='btn btn-primary btn-sm'  data-toggle='modal' data-target='#myModal'   name='[$job_id]' value='[$job_id]'  >  Info</a></td>


            </tr>";
    echo "</form>";
}
echo "
    </tbody>

</table>";

?>

4 个答案:

答案 0 :(得分:2)

<td>
    <select class='form-control col-sm-10' id='status' name='status'>
        <option value='new' <?if($row['status']=='new'){echo "selected";}?>>New</option>
        <option value='progress' <?if($row['status']=='progress'){echo "selected";}?>>Progress</option>
        <option value='wait' <?if($row['status']=='wait'){echo "selected";}?>>Wait</option>
        <option value='done' <?if($row['status']=='done'){echo "selected";}?>>Done</option>
        <option value='close' <?if($row['status']=='close'){echo "selected";}?>>Close</option>
    </select>
</td>

答案 1 :(得分:2)

检查带有选项值的PHP变量,如果匹配,则为此添加atttribute selected='selected'

<option value='new' <?php if($row['status'] == 'new') { echo "selected='selected'" ; } ?> >New</option>
<option value='progress' <?php if($row['status'] == 'progress') { echo "selected='selected'" ; } ?>>Progress</option>
<option  value='wait' <?php if($row['status'] == 'wait') { echo "selected='selected'" ; } ?> >Wait</option>
<option value='done' <?php if($row['status'] == 'done') { echo "selected='selected'" ; } ?>>Done</option>
<option value='close' <?php if($row['status'] == 'close') { echo "selected='selected'" ; } ?> >Close</option>

答案 2 :(得分:1)

如上所述,您可以检查$row['status']的值,并选择性地将所选属性添加到正确的选项中:

"<td>
    <select class='form-control col-sm-10' id='status' name='status'>
        <option value='new' ". ($row['status'] == 'new'? 'selected ': '') .">New</option>
        <option value='progress' ". ($row['status'] == 'progress'? 'selected ': '') .">Progress</option>
        <option  value='wait' ". ($row['status'] == 'wait'? 'selected ': '') .">Wait</option>
        <option value='done' ". ($row['status'] == 'done'? 'selected ': '') .">Done</option>
        <option value='close' ". ($row['status'] == 'close'? 'selected ': '') .">Close</option>
    </select>
</td>"

答案 3 :(得分:0)

如果您需要选择特定选项,则需要将selected attribute设置为选项标记:

<option value='new' <?if($row['status']=='new'){echo "selected";}?>>New</option>