Jump to content

[SOLVED] Optional Form Fields


r00tk1LL

Recommended Posts

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.

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

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

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.

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??

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

I would use thorpe's suggestion:

 

if (isset($_POST['name']) && (!empty($_POST['name']))

 

Are you saying that when you use the above suggestion it doesn't work, or are you just saying that they both work but you think the second one is a better way?

;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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.