mysql下拉框回显,当从MySQL数据库使用下拉框时显示一行中的多个值

I'm trying to show a specific item in the row when an item in the dropdown list is selected.

To clarify, lets say I have item1 chosen in the dropdown menu, and when item1 is chosen, I want price for item1 shown in another field on the page.

ps. I'm trying to make an inventory and ordering form which is connected to an MYSQL database.

Thanks in advance.

Here is my PHP code.

function dropdown( $alcohol, array $options, $selected=null )

{

/*** begin the select ***/

$dropdown = ''."\n";

$selected = $selected;

/*** loop over the options ***/

foreach( $options as $key=>$option )

{

/*** assign a selected value ***/

$select = $selected==$key ? ' selected' : null;

/*** add each option to the dropdown ***/

$dropdown .= '

`value="'.$key.'"'.$select.'>'.$option.'

'."\n";

}

/*** close the select ***/

$dropdown .= ''."\n";

/*** and return the completed dropdown ***/

return $dropdown;

}

?>

mysql_connect('localhost', 'root', '');

mysql_select_db('');

$sql = "SELECT alcohol FROM alcohol ";

$result = mysql_query($sql);

echo "";

while ($row = mysql_fetch_array($result)) {

echo "" . $row['alcohol'] . "";

}

echo "";

?>

//NEW STUFF BELOW THIS//

--- Select Alcohol ---

mysql_connect('localhost', 'root', '');

mysql_select_db('');

$sql = "SELECT alcohol FROM alcohol";

$result1 = mysql_query($sql);

while ($row = mysql_fetch_array($result1)) {

echo "" . $row['alcohol'] . "";

}

$dropdown1 = empty($_POST['alcohol'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['alcohol']);

echo "";

?>

if(isset($_POST['Submit'])) {

mysql_connect('localhost', 'root', '');

mysql_select_db('');

$sql = "SElECT * FROM alcohol where '$dropdown1' = alcohol";

$result = mysql_query($sql) or die(mysql_error());

?>

while ($row = mysql_fetch_array($result)) {

echo "

".$row['alcohol']."".$row['price']."".$row['quantity_in_stock']."";

}

}

?>

解决方案

First of all, use mysqli. Mysql_* is deprecated and will be removed in a future version.

Now that we have that out of the way, I would add a "marker" character in the original dropdown select values in order to get the value. Pick a Unicode character that wouldn't normally be in your database, and echo that in the value so it looks like this:

$name

Then in the code above, split the value at the @ character and pick out what is after it in order to get the secondary value. That should save you a database operation in the long run.

Good luck!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值