从选择列表中的mysqli_query显示设定值

时间:2014-08-18 23:56:12

标签: php html mysqli

我有一个表单,用于捕获用户名和类型(控制用户有权访问的内容),并将其写入数据库。类型由选择框控制。目前只有三个值可能:1,4和5。

<?php

$sql1 = "SELECT * FROM `usertype` ORDER BY `id`";
$result1 = mysqli_query($con, $sql1);

?>
<form action="" method="post">
    <ul>
        <li>
            Username:<br>
            <input type="text" name="username">
        </li>
        <li>
            User Type:<br>
            <select name="type">
            <?php
                while ($data1 = mysqli_fetch_assoc($result1)) {
                    echo '<option value="' . $data1['id'] . '">' . $data1['type'] . '</option>';
                }
            ?>
            </select>
        </li>
    </ul>
</form>

我有另一个页面,我想在其中检索单个现有用户,以便可以更改其用户名或类型。我遇到的问题是选择框。它没有显示数据库中的内容。对于我正在测试的特定用户,选择框应该显示类型4.而不是它显示类型5.第二页的代码如下。我做错了什么?

<?php
$user_id = 47;

$sql1 = "SELECT * FROM `usertype` ORDER BY `id`";
$result1 = mysqli_query($con, $sql1);

$sql2 =
"SELECT *
FROM `users`
WHERE `users`.user_id = $user_id";

$result2 = mysqli_query($con, $sql2);
$user_details = mysqli_fetch_assoc($result2);
print_r_html($user_details);
?>
<form action="" method="post">
    <ul>
        <li>
            Username:<br>
            <input type="text" name="username" value="<?php echo $user_details['username'];?>">
        </li>

        <li>
            <select name="type">
            <?php
                while ($data1 = mysqli_fetch_assoc($result1))
                {
                    echo '<option value="' . $data1['id'] . '" success="' . $user_details['type'] . '">' . $data1['type'] . '</option>';
                }
            ?>
            </select>
        </li>
    </ul>
</form>

这是$ user_details的数组哑:

Array
(
    [user_id] => 47
    [username] => paul
    [password] => 5f4dcc3b5aa765d61d8327deb882cf99
    [first_name] => Paul
    [last_name] => Smith
    [email] => paul@somewhere.com
    [email_code] => dda1f2a2c5b29f3b28827716c2dffe61
    [active] => 1
    [password_recover] => 0
    [type] => 4
    [allow_email] => 1
    [profile] => images/profile/6dc76117b3.jpg
    [acct_nbr] => XXXXXXXXX (intentionally masked)
)

选择列表中包含所有值,只是错误的列表显示为已选中。

P.S。我知道表格中还没有提交按钮。

感谢。

2 个答案:

答案 0 :(得分:0)

感谢showdev让我指向正确的方向。下面的代码完成了我所需要的。它取代了问题中发布的第二个代码块中的表单。

<form action="" method="post">
    <ul>
        <li>
            Username:<br>
            <input type="text" name="username" value="<?php echo $user_details['username'];?>">
        </li>

        <li>
            <select name="type">
            <?php
                while ($data1 = mysqli_fetch_assoc($result1))
                {
                    if ($data1['id'] == $user_details['type']) {
                        echo '<option value="' . $data1['id'] . '" selected="selected">' . $data1['type'] . '</option>';
                    } else {
                        echo '<option value="' . $data1['id'] . '">' . $data1['type'] . '</option>';
                    }
                }
            ?>
            </select>
        </li>
    </ul>
</form>

答案 1 :(得分:0)

我做这样的事情。您想检查用户是否输入=数据类型。不确定&#39;成功=&#39;是。

   <form action="" method="post">
        <ul>
            <li>
                Username:<br>
                <input type="text" name="username" value="<?php echo $user_details['username'];?>">
            </li>

            <li>
                <select name="type">
                <?php
                    while ($data1 = mysqli_fetch_assoc($result1))
                    {
                        $selected = ($user_details['type'] == $data1['type']) ? 'selected="selected"' : false;
                        echo '<option value="' . $data1['id'] . '" '.$selected.'>' . $data1['type'] . '</option>';
                    }
                ?>
                </select>
            </li>
        </ul>
    </form>