使用PHP插入Mysql - 不允许空格后的字符。

时间:2012-08-22 10:23:05

标签: php mysql

我有一个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 
?>

我正在寻找正确的代码,以便在提交回数据库时允许空格

谢谢

1 个答案:

答案 0 :(得分:6)

将您的代码修改为:

echo '<option value="' . $nt[category] . '">' . $nt[category] . '</option>';

问题是选项值。 它应该工作。