douyinlai2169 2015-12-10 01:36
浏览 27

使用PHP,SQL数据库填充选择元素

So I have this form below in a html file named searchform.html There are three search elements a Title search field using text input, a Year Number element and a Publisher select element.

        <form id = "SearchForm" method = "get">
        <form action="searchget.php" method="get">

            <p>CD Title</p>
            <input type = "search" name = "title" /><br>
            <p>CD Publisher</p>
            <select name="publisher">
                <?php include 'database_conn.php';      // make db connection
                $sql = "SELECT pubName FROM `nmc_cd`
                    INNER JOIN nmc_category
                    ON nmc_category.catID = nmc_cd.catID
                    INNER JOIN nmc_publisher
                    ON nmc_publisher.pubID = nmc_cd.pubID";

                while ($row = mysql_fetch_array($sql)){
                echo "<option value=\"publisher\">" . $row['pubName'] . "</option>";} ?>
            </select><br>
            <p>CD Year</p>
            <input type = "number" name = "year" />
            <input type = "submit" value = "Search" />
        </form>

All of the elements have been named such as name="title" and the title and year elements entered are appearing in the search URL. However the select is not and its also not populating from my database.

<?php  include 'database_conn.php';      // make db connection

        $pCDTitle = $_GET['title'];
        $pCDYear = $_GET['year'];
        $hPub = $_GET['publisher'];

    $sql = "SELECT pubName, CDYear, CDTitle 
    FROM `nmc_cd`
    INNER JOIN nmc_category
    ON nmc_category.catID = nmc_cd.catID
    INNER JOIN nmc_publisher
    ON nmc_publisher.pubID = nmc_cd.pubID
    WHERE CDTitle = '$pCDTitle' AND CDYear = '$pCDYear' AND pubName <= '$hPub'";

        $rsCD = mysqli_query($conn, $sql) or die(mysqli_error($conn));

        while ($row = mysqli_fetch_assoc($rsCD)) {
              $CDTitle = $row['CDTitle'];
              $CDYear = $row['CDYear']; 
              $pubName = $row['publisher']; 
        }
              mysqli_free_result($rsCD);
          mysqli_close($conn); ?>

I am quite new to PHP and can't figure out whats going wrong here I was taught this recently and struggled with this part of the form

     while ($row = mysql_fetch_array($sql)){
     echo "<option value=\"publisher\">" . $row['pubName'] . "
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染