
- You are not logged in. | Login
November 16, 2006 9:15 pm
- sirburpsalot
- Member


problems with quotes in mysql data to form
Hello!
I need some help, I've got a problem with quotes in data I'm reading from a MySQL database in an html form. I use addslashes() before I write the data to the database, and stripslashes() when I output it to the webpage, but my form is messing up somehow.
For example, i read in a list of sizes in inches (") to display in a select form:
$possible_sizes = get_sizes();
printr( $possible_sizes ); ## (gives me 12\", 10\", and 8\")
foreach ($possible_sizes as $key=>$size) {
printr( stripslashes($size) ); ## (gives me 12", 10", and 8" like I want)
}
echo "<select name=\"size_form\">";
foreach ($possible_sizes as $key => $size) {
echo "<option name=\"$key\" value=\"". stripslashes($size) ."\"></option>\r\n";
}That outputs the form, but I only get the first option from the list, and it's only showing ' 12 ' instead of ' 12" '
When I view the source of the page, I see this:
<option name='0' value="12"">
I can see why that doesn't work with the extra " in there, but how do I display the quote without breaking the option tag? Even if I don't use stripslashes() it still breaks.
November 17, 2006 4:33 pm
- IBdaMac
- Member


Re: problems with quotes in mysql data to form
The trouble is the " in the html tag, html doesn't care about the \ excape characters.
You need to either put it in single quotes ( ' ) or convert it to the html entity (")
November 17, 2006 4:35 pm
- phppat
- Member


Re: problems with quotes in mysql data to form
Your best option would be the second one mentioned, converting to html entities. This will save you headache as you can convert both single *and* double quotes, and ensures your HTML output doesn't break from an unexpeected quote.
Use this code to translate your strings:
foreach ($possible_sizes as $key => $size) {
$size = htmlentities($size, ENT_QUOTES);
echo "<option name=\"$key\" value=\"". stripslashes($size) ."\"></option>\r\n";
}Hope that helps!
Last edited by phppat (November 17, 2006 4:36 pm)
PHP monster


