Database generated listbox having problems with spaces

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Database generated listbox having problems with spaces

Post by davidtube »

I have a list box which is populated from a database by a list of games consoles. So there's PS2, PS3, Nintendo DS, Xbox and XBox 360. A default is selected from the database also. I've found it works fine unless there is a space in the name and the default is select, in which case everything after the space goes missing from the sql update query. If an option with a space is selected, but it isn't the default, it works ok.

Any ideas?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Can we see some code? Sounds like the error is in your php or insert.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Do not forget to put the value in quotes:

<option value="Nintendo DS">Nintendo DS</option>

Below is wrong and will display Nintendo only in your db:

<option value=Nintendo DS>Nintendo DS</option>
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Post by davidtube »

ok, here's the relevant parts

Code: Select all

$title=mysql_real_escape_string($_REQUEST[title]);

$editproduct="UPDATE product SET title='$title', headline='$headline', description='$description', info='$info', imageurl='$imageurl', catagory='$catagory', subcat='$subcat' WHERE title='$_GET[title]' AND subcat='$_GET[subcat]'";

mysql_query($editproduct,$con);
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

davidtube wrote:ok, here's the relevant parts

Code: Select all

$title=mysql_real_escape_string($_REQUEST[title]);

$editproduct="UPDATE product SET title='$title', headline='$headline', description='$description', info='$info', imageurl='$imageurl', catagory='$catagory', subcat='$subcat' WHERE title='$_GET[title]' AND subcat='$_GET[subcat]'";

mysql_query($editproduct,$con);
You defined a variable $title and passed it to mysql_real_escape_string() -- although you left out the quotes around "title" -- but then you didn't use the variable in your SQL string...why??
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Post by davidtube »

I'm probably misunderstanding you but am I not using the variable $title here?

Code: Select all

SET title='$title'
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

davidtube wrote:I'm probably misunderstanding you but am I not using the variable $title here?

Code: Select all

SET title='$title'
Yes, there, but not in the WHERE clause.

Actually, I just looked at your SQL again and there's no need to SET the title, since you're updating the record WHERE that's already the value in the field. It doesn't hurt anything, but it also doesn't accomplish anything.
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Post by davidtube »

Oh. That's just because of the code changing as I've added new things. It still works as it's supposed to, unless there's space when the "subcat" is being posted.

I set the title so the WHERE can find the current title, then it can be changed.
davidtube
Forum Commoner
Posts: 79
Joined: Sun Mar 25, 2007 8:42 pm

Post by davidtube »

I've sorted it. In another piece of code, I'd put

"$subcat'

(one single and one double quote)

thanks for your help
Post Reply