【发布时间】:2017-04-09 11:46:40
【问题描述】:
我创建了一个下拉列表来选择值:
<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.
?>
【问题讨论】:
-
echo "<option value =\"VALUE\">"因为您将实际值设置为VALUE -
用任何参数或变量替换VALUE...
echo "<option selected value="'.$selectedrow['countryName'].'">" . $selectedrow['countryName']."</option>"; } -
试试这个 echo "";
-
mysql_*函数自 PHP 5.5 起已被弃用(并且在 PHP 7 中完全删除),如果可以的话,您应该stop using them。您应该选择另一个允许您使用 prepared statements 的 API(在处理变量时您确实应该使用它),例如mysqli_*或 PDO - 请参阅 choosing an API。