Jump to content

[SOLVED] Optional Form Fields


r00tk1LL

Recommended Posts

I have a form that users can use to search with multiple fields but sometimes they wont search in all the provided fields. What is the best way to have php query the DB with only what has been submitted instead of sending blank html fields??

 

Thanks

Link to comment
Share on other sites

You can do something like this:

 

<?php

$query = "SELECT * FROM table WHERE 1";

if (isset($_POST['optional_field'])) $query .= " AND optional_field = '{$_POST['optional_field']}";
if (isset($_POST['other_field'])) $query .= " AND other_field = '{$_POST['other_field']}";

$result = mysql_query($query)or die(mysql_error());

?> 

 

Check if they submitted that field, and if they did just add to the query.

Link to comment
Share on other sites

I guess the question is can LIKE be used more than 2 times in a query statement?

Would this be the correct syntax:

$query .= " AND other_field LIKE '%{$_POST['other_field']}%' AND other_field LIKE '%{$_POST['other_field']}%'

Could you help me clean this up???

Thanks

Link to comment
Share on other sites

Hey thanks for helping me, I'm trying this without the LIKE to simplify things. This is not working, is there anything wrong....

//some previous code
$link = mysql_connect ( $hostname, $username, $password);
//user input variables
$query="SELECT uid, image, uname FROM mypages WHERE";
if (isset($_POST['name'])) $query .= " name = '{$_POST['name']}";
if (isset($_POST['age'])) $query .= " AND age = '{$_POST['age']}";
if (isset($_POST['location'])) $query .= " AND location = '{$_POST['location']}";
if (isset($_POST['marital'])) $query .= " AND marital = '{$_POST['marital']}";
if (isset($_POST['hobbies'])) $query .= " AND hobbies = '{$_POST['hobbies']}";
if (isset($_POST['interests'])) $query .= " AND interests = '{$_POST['interests']}";
if (isset($_POST['sex'])) $query .= " AND sex = '{$_POST['sex']}";
//rest of code

 

heres the page so you can see what I'm talking about:

http://www.mynorthtexas.com/mypages/final

Link to comment
Share on other sites

Oops, in your initial query, I think you need to set at least one condition.

 

$query="SELECT uid, image, uname FROM mypages WHERE";

 

So you need to add something after the WHERE. Do you have any field that isn't optional? If you do, then you should put it there.

Link to comment
Share on other sites

I see where this is going, could I reference a field that would always be true? For example anyone that has information in the database has a user id 'uid' field...

I.E.

$query="SELECT uid, image, uname FROM mypages WHERE uid = true";

 

Or just some kind of trick to allow the user to enter information in any field keeping them all optional??

Link to comment
Share on other sites

OK I'm playing with this idea. I found that I can use this:

$query="SELECT uid, image, uname FROM mypages WHERE id > 0";
if (isset($_POST['age'])) 
{
$query .= " AND age = '{$_POST['age']}'";
}

And it will work fine no matter what variable I use

But if I use more than one:

$query="SELECT uid, image, uname FROM mypages WHERE id > 0";
if (isset($_POST['age'])) 
{
$query .= " AND age = '{$_POST['age']}'";
}
if (isset($_POST['name'])) 
{
$query .= " AND name = '{$_POST['name']}'";
}

The search fails every time no matter what

 

Please someone tell me what this is all about, I've been on this for days.

Thanks

Link to comment
Share on other sites

;D ;D ;D ;D ;D

Ok Guys this is what I was looking for, I guess I'll stick with this for now since it is working.

 

$query="SELECT uid, image, uname FROM mypages WHERE id > 0";

if ($_POST['name'] != NULL) $query .= " AND name LIKE '%{$_POST['name']}%' || uname LIKE '%{$_POST['name']}%'";
if ($_POST['age'] != NULL) $query .= " AND age = '{$_POST['age']}'";
if ($_POST['location'] != NULL) $query .= " AND location LIKE '%{$_POST['location']}%'";
if ($_POST['marital'] != NULL) $query .= " AND marital = '{$_POST['marital']}'";
if ($_POST['hobbies'] !=NULL) $query .= " AND hobbies LIKE '%{$_POST['hobbies']}%'";
if ($_POST['interests']!=NULL) $query .= " AND interests LIKE '%{$_POST['interests']}%'";
if ($_POST['sex'] !=NULL) $query .= " AND sex = '{$_POST['sex']}'";

 

Thank you guys for your help!!!

;D ;D ;D ;D ;D ;D ;D ;D ;D ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.