每页只有一个SQL动态下拉列表?

时间:2017-05-23 14:05:08

标签: php mysql

我有一个网页(在php中),PHP代码的第一部分获取相关记录。

<?php
    session_start();

    require_once('Connections/default.php');

    if (isset($_POST['command'])){
        $_SESSION['Username'] = '';
        $_SESSION['Password'] = '';
        session_destroy();
    } else {
    }

    if (is_null($_POST['Username'])){
    } else {
        $_SESSION['Username'] = $_POST['Username'];
    }

    if (is_null($_POST['Password'])){
    } else {
        $_SESSION['Password'] = md5($_POST['Password']);
    }

    $sql = "SELECT * FROM Users WHERE Username LIKE '".$_SESSION['Username']."'     AND Password LIKE '".$_SESSION['Password']."'";
    $newdefectquery = "SELECT * FROM Defects WHERE Defect_Status LIKE 'New'";
    $selectallusers = "SELECT * FROM Users";

    $result = $conn->query($sql);
    $defects_new = $conn->query($newdefectquery);
    $allusers = $conn->query($selectallusers);
?>

页面的这一部分正常工作并提取结果(我可以看到页面上的其他数据。)然后我有以下代码部分,其中包含所有网站用户的下拉菜单(所以我可以分配每个用户的缺陷),但只有一个动态下拉菜单有效吗?它显示每个用户。 (如果我有超过1个缺陷,我希望能够分配任何缺陷。)

<?php if ($defects_new->num_rows > 0){
    while ($defect = $defects_new->fetch_assoc()){ ?>
        <table border="1" width="90%">
            <tr>
                <td colspan="2">
                    <h2 style="margin:0px; font-size:25px; line-height:25px;">#<?php echo $defect['Defect_ID'] ?> - <?php echo $defect['Title'] ?> - <?php echo $defect['Found_By'] ?></h2>
                </td>
            </tr>
            <tr>
                <td width="80%">
                    <?php echo $defect['Information'] ?>
                    <br />
                    <br />
                </td>
                <td>
                    <form name="assign<?php echo  $defect['Defect_ID'] ?>" method="post" action="index.php">
                        <select name="Owner">
                        <?php while ($users = $allusers->fetch_assoc()){ ?>
                            <option value="<?php echo $users['Username']?>"><?php echo $users['Username'] ?></option>
                        <?php } ?>
                        </select>
                    </form>
                </td>
                </tr>
            </table>
            <br />
            <br />
<?php } 
} else {
    echo "There are no new defects. Good job! :)";
}
?>

这是我看到的照片。 (我已经扩展了工作下拉列表)enter image description here

第二次下拉不会扩大。它也没有在inspect元素上显示任何选项。

请帮忙。感谢。

2 个答案:

答案 0 :(得分:0)

<?php if ($defects_new->num_rows > 0){

$users = $allusers->fetch_assoc();

while ($defect = $defects_new->fetch_assoc()){ ?>
    <table border="1" width="90%">
        <tr>
            <td colspan="2">
                <h2 style="margin:0px; font-size:25px; line-height:25px;">#<?php echo $defect['Defect_ID'] ?> - <?php echo $defect['Title'] ?> - <?php echo $defect['Found_By'] ?></h2>
            </td>
        </tr>
        <tr>
            <td width="80%">
                <?php echo $defect['Information'] ?>
                <br />
                <br />
            </td>
            <td>
                <form name="assign<?php echo  $defect['Defect_ID'] ?>" method="post" action="index.php">
                    <select name="Owner">
                    <?php foreach($users as $user){ ?>
                        <option value="<?php echo $user['Username']?>"><?php echo $user['Username'] ?></option>
                    <?php } ?>
                    </select>
                </form>
            </td>
            </tr>
        </table>
        <br />
        <br />
<?php } 
} else {
    echo "There are no new defects. Good job! :)";
}
 ?>

答案 1 :(得分:0)

从第一个代码部分删除用户查询执行部分:

export * from 'YourComponentRoute

在弹出数据之前运行查询:

<?php
    session_start();

    require_once('Connections/default.php');

    if (isset($_POST['command'])){
        $_SESSION['Username'] = '';
        $_SESSION['Password'] = '';
        session_destroy();
    } else {
    }

    if (is_null($_POST['Username'])){
    } else {
        $_SESSION['Username'] = $_POST['Username'];
    }

    if (is_null($_POST['Password'])){
    } else {
        $_SESSION['Password'] = md5($_POST['Password']);
    }

    $sql = "SELECT * FROM Users WHERE Username LIKE '".$_SESSION['Username']."'     AND Password LIKE '".$_SESSION['Password']."'";
    $newdefectquery = "SELECT * FROM Defects WHERE Defect_Status LIKE 'New'";
    $selectallusers = "SELECT * FROM Users";

    $result = $conn->query($sql);
    $defects_new = $conn->query($newdefectquery);
    //$allusers = $conn->query($selectallusers);
?>

一旦查询列出所有记录,它将返回null,因为没有任何显示。您可以在第一个代码部分中获取数组中的所有用户列表,并且可以循环该用户数组,以便它将执行一次。