Problem with passing a variable in the URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Problem with passing a variable in the URL

Post by dajawu »

Ok so I have a simple bit of code that takes a variable out of the URL using $_GET['Company']; and passing it into an SQL statement. So far it works fine unless the variable has an ampersand symbol in it. If it does it cuts the variable off at the symbol. For example if the URL was

test.php?Company=Company%20test1

the variable would be 'Company test1'. But if the URL was:

test.php?Company=H&H%20Manufacturing

the variable would be 'H'

It always cuts it off at the ampersand. I have tryed passing an actual '&' symbol and I have tried passing & but neither work. Can you guys help me out?

Thanks in advance, and hello everyone! I just signed up as I am an asspiring PHP coder trying to make a name for myself!

Al
User avatar
puke7
Forum Newbie
Posts: 12
Joined: Sat May 10, 2008 11:49 am

Re: Problem with passing a variable in the URL

Post by puke7 »

the & separates variables in the URL
URLs are also not savvy with HTML entities like &
use php functions urlencode() & urldecode() to turn symbols into parsable hex values and back again
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Re: Problem with passing a variable in the URL

Post by N1gel »

PHP provides these functions to do it

urldecode

urlencode
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Re: Problem with passing a variable in the URL

Post by dajawu »

Wow it separates the variables, that’s right! Amateur mistake, thanks guys!
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Re: Problem with passing a variable in the URL

Post by highjo »

i think you should use urlencode before you pass the variable through querystring.the ampersand between H&H is seen as a start of another variable.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Problem with passing a variable in the URL

Post by RobertGonzalez »

You should also be escaping any data that touches your database. Always.
Post Reply