php form

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

User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

You must quote strings inside the SQL.

Code: Select all

"SELECT `foo` FROM `bang` WHERE name = %s"
becomes

Code: Select all

"SELECT `foo` FROM `bang` WHERE name = Foobar"
Which is incorrect, it should be:

Code: Select all

"SELECT `foo` FROM `bang` WHERE name = 'Foobar'"
olm75
Forum Newbie
Posts: 16
Joined: Fri Aug 05, 2005 8:05 pm

sorry

Post by olm75 »

no change at all
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what did you change?
olm75
Forum Newbie
Posts: 16
Joined: Fri Aug 05, 2005 8:05 pm

ok

Post by olm75 »

i think my problem mostly lies in the query itself...this is what i have:

Code: Select all

SELECT venuename, address, city, `state`, zip, phone, areacode
FROM venues_new
WHERE venuename LIKE 'MMColParam%' AND areacode = 'varAC'



but what i want to do is allow the viewer to search with 1 or any combination of fields in the search form...to get results or narrow results, what this query is doing is making them choose both fields without having the option to choose just one...if they want a much broader search....and i have 4 more list/menu search fields to add on....
here is the form:
http://www.nightspotz.com/search1.php
Last edited by olm75 on Sun Aug 14, 2005 2:54 am, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$query_rsSearch = sprintf("SELECT venuename, address, city, `state`, zip, phone, areacode FROM venues_new WHERE venuename LIKE '%%s%' AND areacode = '%s'", $MMColParam_rsSearch,$varAC_rsSearch);
Notice the change in

...venuename LIKE '%%s%' AND area.....

Edit | Feyd's way is the correct way :oops:
Last edited by John Cartwright on Sun Aug 14, 2005 1:49 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

*cough*

Code: Select all

$query_rsSearch = sprintf("SELECT venuename, address, city, `state`, zip, phone, areacode FROM venues_new WHERE venuename LIKE '%%%s%%' AND areacode = '%s'", $MMColParam_rsSearch,$varAC_rsSearch);
:oops: excuse me :o .. I must get rid of this dastardly cold. ooo.
olm75
Forum Newbie
Posts: 16
Joined: Fri Aug 05, 2005 8:05 pm

i'm sorry

Post by olm75 »

how will that translate to this:

Code: Select all

SELECT venuename, address, city, `state`, zip, phone, areacode
FROM venues_new
WHERE venuename LIKE 'MMColParam%' AND areacode = 'varAC'
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it doesn't.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

By the way, the backticks that I had in my SQL is just a little habit I picked up from analyzing PHPMyAdmin dumps to much. They enclose tablenames and the like, and are strictly optional, and I just add 'em in anyway for not much reason.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Actually they are not always optional and can avoid certain problems... best to always use them.
olm75
Forum Newbie
Posts: 16
Joined: Fri Aug 05, 2005 8:05 pm

need example

Post by olm75 »

does any on have a full working example or tutorial of a search page that allows the viewer to search by 1 or any combination of fields....search must have at least 5 fields on it.....for some reason i cant do a query with more than combination of 2 fields....
Post Reply