我是php的新手,我有一张表customer_name
如何在我的页面中将此客户名称作为组合框。
然后我点击提交只存储id
请查询并编码此
答案 0 :(得分:1)
您可以选择id
和name
。
SELECT `id`, `name` FROM `customer_name`
然后,您将回显select
,将value
属性设置为id
,将文本节点设置为name
。
这将在表单提交上传送id
。
答案 1 :(得分:0)
显示来自db的选择框就像这样
<select name="categoryID">
<?php
$sql = "SELECT customer_id, customer_name FROM customers ".
"ORDER BY customer_name";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
echo "<option value=\"".$row['customer_id']."\">".$row['customer_name']."</option>";
}
?>
</select>
阅读more
答案 2 :(得分:0)
首先,Don't use mysql_*
functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDO或MySQLi - 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;
?>
答案 3 :(得分:0)
我认为这可能对你有帮助。
<?php
// make connection with database
$db_name = "test";
$connection = mysql_connect('localhost','root','') or die(mysql_error());
$db = mysql_select_db($db_name,$connection) or die(mysql_error());
$sql = "SELECT customer_id, customer_name FROM customer_name";
$result = mysql_query($sql);
// start process of making combo box
$comboBox = "<select name='cust_name'>";
while ($row = mysql_fetch_array($result)) {
// add option with list
$comboBox .= "<option value='". $row['customer_id'] ."'>". $row['customer_name'] ."</option>";
}
$comboBox .= "</select>";
// echo this comboBox variable where you what to display this comboBox. like this
echo $comboBox;
// when you put this combo box within form you can get vlaue of this comboBox like this
// $_POST['cust_name']; // this return customer id of selected customer
?>