mysql get one field from one table into another page list type
i am new in php, i have one table has customer_name
how t开发者_Go百科o get this customer name in my page as a combo box.
then i click submit only store the id only
please query and code for this
You would select the id
and name
.
SELECT `id`, `name` FROM `customer_name`
You would then echo a select
, placing the value
attribute as id
and the text node as name
.
This will transmit the id
on form submit.
First off, Don't use mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
example code, hope it helps
<?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;
?>
to show select box from db is like this
<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>
read more
I think this may help you.
<?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
?>
精彩评论