Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP + PostgreSQL
#1
If I have a form like:
Search the database
ID: 000
Name: xxxxxxxx
Last: xxxxxxx
Age: 00

And the person can fill in any or all of the fields and push "search", and a query goes off to the database, how do I make it work if they don't fill in all the fields?
Reply
#2
have a query building structure like
Code:
$query = "blah blah blah WHERE ";
if (isset($_POST['fname'])){
  $query .= "name='". $_POST['fname']. "', ";
}
.......etc

I hope that helps a little...
am an asshole. Get used to it.
Reply
#3
It doesn't work!

I had to add single quotes in the third line down, right at the end between the ", "; double quotes, and I'm getting a parse error, I suspect because of the comma.

Now my queries look like SELECT blah FROM blah WHERE userid='', firstname='', lastname='', age=''

What should it look like?
Reply
#4
without the building... it'll be like this
Code:
WHERE name='something', stuff='something'

Sorry, I forgot a ' before the comma.
am an asshole. Get used to it.
Reply
#5
Hmmm... the if(isset( part doesn't seem to work, because I definately don't want blahblah='' do I. Any other way to check for a variable?
Reply
#6
isset checks to see if the variable has been set, even to nothing, so... you should check after the isset for something like
Code:
if (chop($_POST['fname']) != "")
To make sure it doesn't equal just empty space.
am an asshole. Get used to it.
Reply
#7
I'll check it later, when I'm in Linux (stupid bio project :wink: )

But if it works, thanks alot!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)