从动态下拉菜单中检索表单输入的值

时间:2014-01-22 01:44:14

标签: php mysql select

我有一个具有动态下拉菜单的表单,该菜单从经销商表中检索dealerhip_id,一旦选择下拉列表并提交表单,它就会将该值插入数据库的另一个表中。这在我的服务器上运行正常。

我要做的是检索" users_dealer_name"在同一行中的值,使其成为一个变量($ dealer_name),然后在电子邮件中打印该变量,即如果它检索到id = 3我想要它打印" Mike Chan"但它空白了。我哪里错了?

经销商数据库样本

dealership_id | users_dealer_name 
1             | John Smith
2             | Jane Smith
3             | Mike Chan

验证码

<?php 
if (isset($_POST['dealership_id'])) {
$dealer_id = (int) $_POST['dealership_id'];

$query2 = 'SELECT users_dealer_name FROM dealership WHERE dealership_id=$dealer_id';
$dealer_name = @mysql_query($query2);

} else {
$dealer_id = 0;
}
if ($dealer_id > 0) {
$query = "SELECT dealership_id
          FROM dealership 
          WHERE dealership_id=$dealer_id";
$result = mysql_query ($query); }
else { echo '<p><font color="red">Please select your Dealership </font></p>'; }
?>  

动态下拉

<?php 
require_once ('database.php');  
echo '<select name="dealership_id"><option value="NULL">Choose a Dealer:</option>'; 
$query = 'SELECT * FROM dealership ORDER BY users_dealer_name ASC';
$result = mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<option value=\"$row[0]\" ";
if (isset($_POST['dealership_id']) && $_POST['dealership_id'] == $row[0]){
    echo ' selected=\"selected\"';
}
echo ">$row[3]</option>";
}
echo '</select> ';
mysql_close();  
?>

0 个答案:

没有答案