-4

我是编程新手。您能帮我从 MySQL 获取数据到 PHP 列表/菜单吗?

<tr>
    <td align="right">Owners Name</td>
    <td>
        <select name="owners" id="owners">

            <?php 

            $owners = mysql_real_escape_string($_POST['owners']);
            $sql = mysql_query("SELECT lld_Lname,lld_Fname FROM landlords "); 

            while ($row = mysql_fetch_array($sql))
            {
            ?> 

            <option <?php echo $_POST['owners']==$row['owners'] ? 'selected' : ''?>><?php echo $row['owners'] ?></option> 


            <?php 
            } 
            ?> 

        </select>
    </td>
</tr>

运行上面的代码后,我得到这个错误:

注意 :undefined index:owners in c:\wamp\www\real_estate\admin\add_building.php online 41

4

2 回答 2

1

只需 print_r 您的 $row 并检查获取了哪些所有数据。检查 [owners] 是否存在(在您的情况下为否),然后进一步进行比较

于 2013-06-26T09:58:15.110 回答
0
    <?php 
    $owners = mysql_real_escape_string($_POST['owners']);
    $sql = mysql_query("SELECT owners FROM landlords ") or die(mysql_error());
    ?>
    <tr> 
    <td align="right">Owners Name</td> 
    <td><select name="owners" id="owners">

        <?php
        while ($row = mysql_fetch_array($sql)) { ?> 

        <option <?php echo $_POST['owners']==$row['owners'] ? 'selected' : ''?>><?php echo $row['owners'] ?></option> 

        <?php } ?> 

        </select>
    </td>
    </tr>

如果您的数据库表中提供了所有者列,这可能会起作用或发布您遇到的错误

于 2013-06-26T09:46:02.790 回答