PHP下拉数据库代码不起作用?

时间:2016-04-07 18:31:30

标签: php html mysql mysqli

我遇到以下代码的问题,它显示的是html部分,但不显示数据库中的数据。我只是尝试从下拉列表中获取数据库中的数据,当我单击find将其显示在下表中时。你能帮忙吗?

    <?php
    include 'config.php';
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <select name="manufacturer">
            <option value="">----ALL----</option>
        <?php
            $sql = "SELECT id, manufacturer FROM coop";
            $result = $conn->query($sql);
            while ($r = mysqli_fetch_assoc($result))
            {
                echo "<option value='$r[id]'> $r[manufacturer]</option>";
            }
        ?>
        </select>
        <input type="submit" name="find" value="find"/>
        <br><br>

        <table border="1">
            <tr align="center">
                <th>ID</th> <th>Manufacturer</th>
            </tr>

            <?php
            if($_SERVER['REQUEST_METHOD'] == "POST")
                {
                    $des=$_POST["manufacturer"];
                    if($des=="")
                    {
                        $result=  mysqli_query("SELECT id, manufacturer FROM coop");
                    }
                    else
                    {
                        $result= mysqli_query($sql);
                    }
                    echo "<tr><td colspan='5'></td></tr>";
                    while($r= mysqli_fetch_assoc($result))
                    {
                        echo "<tr>";
                        echo "<td align='center'>$r[0]</td>";
                        echo "<td width='200'>$r[1]" . " $r[2]</td>";
                        echo "<td alig='center' width='40'> $r[3]</td>";
                        echo "<td align='center' width='200'>$r[4]</td>";
                        echo "<td width='100' align='center'>$r[5]</td>";
                        echo "</tr>";
                    }
                }
            ?>
        </table>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

你的(?<!,)错了

queries

<?php $sql = "SELECT id, manufacturer FROM coop"; $result = $conn->query($sql); while ($r = mysqli_fetch_assoc($sql)) { echo "<option value='$r[0]'> $r[0]</option>"; } ?> 应为while ($r = mysqli_fetch_assoc($sql))

您应该使用while ($r = mysqli_fetch_assoc($result ))而不是$result

来获取您的关联

下面还有一些错误