I would be great if someone could spot the problem with this code please.
I have a MySQL table that was populates an Options list which works well when adding new records to another database table.
The code is:

$query="SELECT id, types FROM $table ORDER BY types ASC";
$result = mysql_query ($query);
echo "<select name=\"type\">"; 
echo "<option value='Select from list.....'>Select from list.....</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[types]>$nt[types]</option>";
}
echo "</select>";

In an EDIT program I required the Options list item previously selected when adding the record to be 'echo'd back'.
I changed the above code to do this as shown here - See Line 4 changes: ($type is the data field read from the SQL table)

$query="SELECT id, types FROM $table ORDER BY types ASC";
$result = mysql_query ($query);
echo "<select name=\"type\">"; 
echo "<option value=$type>$type</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[types]>$nt[types]</option>";
}
echo "</select>";
?>

When using this code the Options list selected when creating the record is echo'd back correctly using the above code in the Edit program.
BUT if the Options list item cotains a space between 2 words then only the first word is saved and the second word is lost.
So for example BOOK SHOP is saved as BOOK. If I go and edit the data item in the options table from BOOK SHOP to BOOK-SHOP then it is not lost!!

Thanks in advance for any suggestion
Stephen

That happens because you dont know anything basic about HTML...

you CANT do <option value=$type>$type</option> or any value... you must use value="$type"...

Member Avatar
diafol

That happens because you dont know anything basic about HTML...

That's a bit harsh. And in fact you are wrong. It's probably not the html but the php:

$query="SELECT id, types FROM $table ORDER BY types ASC";
$result = mysql_query ($query);
echo "<select name=\"type\">"; 
echo "<option value=\"$type\">$type</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=\"{$nt['types']}\">{$nt['types']}</option>";
}
echo "</select>";

You need to 'brace out' array item variables with { ... } if you want to include them within the double quotes. Also use single quotes to encompass the item name: 'types'

And finally, the html will be better with quotes around the attribute: value="$type". It should not fail unless you insert some weird chars...

The HTML4.0 spec states:

In certain cases, authors may specify the value of an attribute without any quotation marks. The attribute value may only contain letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45), and periods (ASCII decimal 46). We recommend using quotation marks even when it is possible to eliminate them.

Haven't read HTML5 spec yet.

value='$type'  & value='$nt[types]'

its cuting off anything after the space and using the next part as an attribute, as thats the delimiter for attributes in a html element

Thanks ardev that was the solution - sorry for the late reply

Would be like this:

    "<option value='{$type}'>$type</option>"