简体   繁体   English

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

[英]Only one of my SQL dynamic drop downs works per page?

I have a webpage (in php) where the first section of PHP code gets the relevant records. 我有一个网页(在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);
?>

This section of the page works correctly and pulls the results (I can see other data on the page.) I then have the following section of code, which contains a drop down menu of all the site's users (So I can assign defects to each user), but only one of the dynamic drop down menus works? 页面的此部分正常工作并提取结果(我可以在页面上看到其他数据。)然后,我得到以下代码部分,其中包含该站点所有用户的下拉菜单(因此,我可以为每个用户分配缺陷用户),但只有动态下拉菜单之一有效? It displays every user. 它显示每个用户。 (Where I have more than 1 defect, I would like to be able to assign any of them.) (如果我有多个缺陷,那么我希望能够分配其中任何一个。)

<?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! :)";
}
?>

Here's a picture of what I see. 这是我所见的照片。 (I've expanded the working drop down) (我扩大了工作范围) 在此处输入图片说明

The second drop down does not expand. 第二个下拉列表不会扩展。 It also does not show any options on inspect element. 它也没有在检查元素上显示任何选项。

Please help. 请帮忙。 Thanks. 谢谢。

<?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! :)";
}
 ?>

remove user query execution part from first code part: 从第一个代码部分删除用户查询执行部分:

<?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);
?>

run query before you popup data: 在弹出数据之前运行查询:

<?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 $allusers = $conn->query($selectallusers); 
                          $users = $allusers->fetch_assoc();//here
                         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! :)";
}
 ?>

once query list all record, it will return null as there is nothing to show. 一旦查询列出所有记录,由于没有任何显示,它将返回null。 You can get all user list in array in your first code part and you can loop that user array so it will execute one time. 您可以在第一个代码部分中获得数组中的所有用户列表,并且可以循环该用户数组,以使其执行一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM