通过我的SQL下拉列表

时间:2011-03-16 08:17:20

标签: php mysql html

我在一个html页面中有一个组合框另一个页面我要调用数据库。在数据库中选择客户名称这些名称分别在组合框中显示。请为我编写代码。

1 个答案:

答案 0 :(得分:1)

首先,Don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial


<?php 
$db_name = "db";
$connection = mysql_connect('localhost','root','') or die(mysql_error());
$db = mysql_select_db($db_name,$connection) or die(mysql_error());
$sql = "SELECT customer_name,id from customers ORDER BY customer_name desc";
$result = mysql_query($sql,$db) or die(mysql_error());
if(mysql_num_rows($result)>=1){
    $form = '<form method="POST" action="">
    <p>Customer name:<select size="1" name="customer">';
    while ($row = mysql_fetch_array($result)) {
        $form .='<option value="'.$row['id'].'">'.ucwords($row['customer_name']).'</option>';
    }
    $form .=' </select></p><p><input type="submit" value="Submit"></p></form>';
}
echo $form;
?>