我有一个PHP代码,它从mysql表中的字段生成一个列表.当我将信息提交到另一个数据库时,它不会在空格后显示任何内容 - 只是第一个单词(例如,如果我从下拉列表中选择'robe spot'而仅将sumbit'robe'插入到数据库中)
这是我的PHP代码:
<?
$dbHost = 'localhost'; // localhost will be used in most cases
// set these to your mysql database username and password.
$dbUser = 'user';
$dbPass = 'pass';
$dbDatabase = 'stock'; // the database you put the table into.
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("stock") or die(mysql_error());
$query="SELECT category FROM subcategory_table";
/* You can add order by clause to the sql statement if the names are to be displayed
in alphabetical order */
$result = mysql_query ($query);
echo "<select name='category'>";
// printing the list box select command
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[category]>$nt[category]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
Run Code Online (Sandbox Code Playgroud)
我正在寻找正确的代码,因此在提交回数据库时它允许空格
谢谢
将您的代码修改为:
echo '<option value="' . $nt[category] . '">' . $nt[category] . '</option>';
Run Code Online (Sandbox Code Playgroud)
问题是选项价值.它应该工作.