我创建一个下拉列表来选择值:
<form action="." method="post" id="aligned">
<input type="hidden" name="action" value="update_customer">
<input type="hidden" name="customer_id"
value="<?php echo htmlspecialchars($customer['customerID']); ?>">
<label>Country:</label>
<select name="selected">
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass ='';
$db = 'tech_support';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(!$conn)
die('Could not connect: '. mysql_error());
mysql_select_db($db);
$selected= mysql_query("select * from countries where countryCode = '" .$customer['countryCode']. "'");
$sql = mysql_query("select * from countries order by countryName");
if($selectedrow = mysql_fetch_array($selected)){
echo "<option selected value=\"VALUE\">" . $selectedrow['countryName']."</option>";
}
//echo "<select>";
while ($row = mysql_fetch_array($sql)) {
echo "<option value =\"VALUE\">". $row['countryName']."</option>";
}
//echo "</select>";
?>
</select><br>
在下面的另一个php文件中,我试图存储Country的选定值,但没有存储,为什么?
<?php $country_name = $_POST["selected"];
echo $country_name;//it always print out 'VALUE'-I guess it means null.
?>
答案 0 :(得分:0)
你应该使用
echo "<option value ='".$row['countryName']."'>". $row['countryName']."</option>";
我想补充一点,MySQL在PHP5中被弃用,并从PHP7中删除。什么是你的PHP版本。如果您使用PHP 5或更高版本,请使用mysqli。