0

I have a php code which generates a list from a field in mysql table. When I submit the information into another database it doesnt show anything after the space - just the first word (for example, if i choose 'robe spot' from the drop down and sumbit only 'robe' is inserted into the database)

Here is my php code:

<?
$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 
?>

I'm looking for the correct code so it allows spaces when submitting back into the database

Thank You

3
  • where is your insert query?? please post that also Commented Aug 22, 2012 at 10:26
  • It's not a help for the question, but you should stop using mysql functions - they are depricated. Instead you can use Mysqli - php.net/manual/en/book.mysqli.php or PDO - http:php.net/manual/en/book.pdo.php and it's a good practice to surround the fields with `` Commented Aug 22, 2012 at 10:28
  • Yoong Kim has pointed you at a solution. You could have found this problem yourself if you had performed basic automated QA by running your HTML through a validator. Commented Aug 22, 2012 at 10:32

1 Answer 1

6

Modify your code as:

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

The problem is the option value. It should work.

Sign up to request clarification or add additional context in comments.

4 Comments

It would help if you explained the problem was that the attribute value in the generated HTML wasn't quoted. When I first read this I though you were suggesting that using variable interpolation was the problem.
You could also just put apostrophes in the double-quoted string : value = '$nt[category]'.
you could put "{$var}" will look much cleaner
but I wrote the problem: "The problem is the option value". But maybe it was not so clear...but it was so obvious for me, sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.