Populating Select Element using PHP, SQL Database

时间:2015-12-10 01:36:36

标签: php html mysql sql forms

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 个答案:

没有答案