【问题标题】:Populate Dropbox on Webpage with Database Query使用数据库查询在网页上填充 Dropbox
【发布时间】:2014-04-16 18:23:33
【问题描述】:

我正在尝试使用存储在数据库中的信息在我的网页上填充一个保管箱。当我加载页面时,dropbox 是空的,并且不显示 dropbox 后面的任何信息。

用于 DROPBOX 的 PHP:

        <?php 
            $sql_query = mysql_query("SELECT state_abbr, state_name FROM state_t ORDER BY state_name");
            echo '<select name="State">';
            while ($row = myssql_fetch_array($sql_query))
            {
                echo '<option value="'.$row['state_abbr'].'">'.$row['state_name'].'</option>';
            }
            echo "</select>";
        ?>

整个表单元素 HTML

<form
    action="http://webdev.spsu.edu/formtest.php"
    method="post">
    <div class="content">
    <table class="formTable">
        <tr>
            <td><label for="firstname">First Name:&nbsp;</label></td>
            <td><input type="text" name="First_Name" id="firstname" size="25" /></td>
            <td><label for="lastname">Last Name:&nbsp;</label></td>
            <td><input type="text" name="Last_Name" id="lastname" size="25" /></td>
        </tr>
        <tr>
            <td><label for="StreetAdd">Street Address:&nbsp;</label></td>
            <td><input type="text" name="Street_Address" id="StreetAdd" size="25" /></td>
            <td>Select State:&nbsp;</td>
            <td>
                <?php 
                    $sql_query = mysql_query("SELECT state_abbr, state_name FROM state_t ORDER BY state_name");
                    echo '<select name="State">';
                    while ($row = myssql_fetch_array($sql_query))
                    {
                        echo '<option value="'.$row['state_abbr'].'">'.$row['state_name'].'</option>';
                    }
                    echo "</select>";
                ?>
            </td>
            <td><label for="ZipCode">Zip Code:&nbsp;</label></td>
            <td><input type="text" name="Zip_Code" id="ZipCode" size="25" /></td>
        </tr>
        <tr>
            <td><label for="appleorderquantity">Apple Quantity (pounds):&nbsp;</label></td>
            <td><input type="text" name="Apple_Quantity" id="appleorderquantity" size="25" /></td>
            <td><label for="grapeorderquantity">Grape Quantity (pounds):&nbsp;</label></td>
            <td><input type="text" name="Grape_Quantity" id="grapeorderquantity" size="25" /></td>
            <td><label for="strbryorderquantity">Strawberry Quantity (pounds):&nbsp;</label></td>
            <td><input type="text" name="Strawberry_Quantity" id="strbryorderquantity" size="25" /></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td>Select Payment Method:<br /></td>
            <td>
                <label>MasterCard 
                    <input type="radio" name="Pref_Payment" id="pref_pay_Mcard" 
                        value="MasterCard" checked="checked" />
                </label>
                <br />
                <label>Visa 
                    <input type="radio" name="Pref_Payment" id="pref_pay_Visa" 
                        value="Visa" />
                </label>
                <br />
                <label>American Express
                    <input type="radio" name="Pref_Payment" id="pref_pay_AmEx" 
                        value="American Express" />
                </label>
                <br />
                <label>Discover
                    <input type="radio" name="Pref_Payment" id="pref_pay_Disc" 
                        value="Discover" />
                </label></td>
            <td></td>
            <td></td>
        </tr>
    </table>
    </div>
<div class="submitButton">
    <input type="submit" value="Submit" onClick="return validateOrder()" />
</div>
</form>

这就是显示的内容 这就是我想要展示的

【问题讨论】:

    标签: php html sql database


    【解决方案1】:

    你在这行有一个错字:

    while ($row = myssql_fetch_array($sql_query))
    

    应该是:

     while ($row = mysql_fetch_array($sql_query))
    

    它有助于在调试时查看正在发生的错误。您可以将此行添加到单个脚本以显示所有错误:

    error_reporting(E_ALL);
    

    此外,是时候停止使用已弃用的 mysql 函数了。切换到mysqli 仅用于 MySQL,或者如果您切换到 PDO,您可以与 MySQL 和许多其他数据库类型(MSSQL、Oracle、PostgreSQL、SQLite 等)进行交互。

    【讨论】:

    • 解决了显示问题,但下拉菜单仍然是空的。
    • 您是否连接到表单代码上方的数据库?在任何地方都可以打到mysql_connect 吗?
    • 啊,你用的是PostgreSQL数据库吗?
    • 是的。我是。我添加了一个if (!$sql_query){die("Database error...");},它在下拉菜单之后就死了,所以我猜我的 php 连接到数据库不正确......???
    • 是的,mysql 函数只适用于 mysql 数据库。你需要的是整套pg functions from the documentation
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 2020-11-13
    • 1970-01-01
    相关资源
    最近更新 更多