...

View Full Version : Search From Help...



tomyknoker
07-15-2007, 09:00 AM
Hi all... I have a search form which until I added the date was working well, e.g. the user could just enter 'James' in the FIRST NAME, and get a result. I added a search for DATE and now the user must enter a date other wise the results are 0, I want the date to be in the search but if the user just enters first name and no date I want to see the results... Hopefully this makes some sense? Here's my code...


/*set varibles from form */
$firstName = trim($_POST['firstname']);
$lastName = trim($_POST['lastname']);
$email = trim($_POST['email']);
$rep = $_POST['rep'];
$state = $_POST['state'];
$status = $_POST['status'];
$early_Joindate = $_POST['early_Joindate'];
$later_Joindate = $_POST['later_Joindate'];


$results = mysql_query("SELECT * FROM `tblmembers` WHERE `FirstName` LIKE '%$firstName%'
AND `LastName` LIKE '%$lastName%'
AND `Email` LIKE '%$email%'
AND `State` LIKE '%$state%'
AND `MemberApproved` LIKE '%$status%'
AND `rep_NBR` LIKE '%$rep%'
AND `JoinDate` >= '$early_Joindate' && `JoinDate` <= '$later_Joindate'
ORDER BY `LastName`");

NancyJ
07-15-2007, 09:18 AM
You need to either default your dates if there are none or just remove the date search entirely if they dont put in a date. Currently you are searching for dates between '' and ''.



/*set varibles from form */
$firstName = trim($_POST['firstname']);
$lastName = trim($_POST['lastname']);
$email = trim($_POST['email']);
$rep = $_POST['rep'];
$state = $_POST['state'];
$status = $_POST['status'];
$early_Joindate = $_POST['early_Joindate'];
$later_Joindate = $_POST['later_Joindate'];

$sql = "SELECT * FROM `tblmembers` WHERE `FirstName` LIKE '%$firstName%'
AND `LastName` LIKE '%$lastName%'
AND `Email` LIKE '%$email%'
AND `State` LIKE '%$state%'
AND `MemberApproved` LIKE '%$status%'
AND `rep_NBR` LIKE '%$rep%'
";
if(!empty($early_Joindate) && !empty($later_Joindate))
{
$sql . = " AND `JoinDate` >= '$early_Joindate' && `JoinDate` <= '$later_Joindate'";
}
$sql .= " ORDER BY `LastName`"

$results = mysql_query($sql);

tomyknoker
07-15-2007, 10:01 AM
Ahhhh perfect I love that idea... What I don't get though (let's remove the date entirely) it if they don't enter say a LastName... And just a firstname the search still works perfectly like it doesn't need a LastName, but needs a date... Hmmm did that make sense??

tomyknoker
07-15-2007, 10:17 AM
Ug I got the following error:

Parse error: syntax error, unexpected T_VARIABLE on line 31

Line 31 is
$results = mysql_query($sql);

Nightfire
07-15-2007, 10:22 AM
Missing semi-colon at the end of the line above

$sql .= " ORDER BY `LastName`";

tomyknoker
07-15-2007, 10:23 AM
Yea found it I shouldn't have been so quick to post! Thanks! :)

NancyJ
07-15-2007, 11:15 AM
Ahhhh perfect I love that idea... What I don't get though (let's remove the date entirely) it if they don't enter say a LastName... And just a firstname the search still works perfectly like it doesn't need a LastName, but needs a date... Hmmm did that make sense??

You're using a LIKE search for the names but you need an exact match (within the given range) for the dates. Saying where name LIKE '%%' will match any name

tomyknoker
07-15-2007, 11:24 AM
Ah ok got it!

I tried to add another date search actually I will have four in the end... I'm not getting any errors but it's giving me results as the entire database...

Do I need to change anything on the second If?


$sql = "SELECT * FROM `tblmembers` WHERE `FirstName` LIKE '%$firstName%'
AND `LastName` LIKE '%$lastName%'
AND `Email` LIKE '%$email%'
AND `State` LIKE '%$state%'
AND `MemberApproved` LIKE '%$status%'
AND `rep_NBR` LIKE '%$rep%'
";
if(!empty($early_Joindate) && !empty($later_Joindate))
{
$sql .= "AND `JoinDate` >= '$early_Joindate' && `JoinDate` <= '$later_Joindate'";
}
if(!empty($early_Approveddate) && !empty($later_Approveddate))
{
$sql .= "AND `MemberApprovedDate` >= '$early_Approveddate' && `MemberApprovedDate` <= '$later_Approveddate'";
}
$sql .= "ORDER BY `LastName`";

$results = mysql_query($sql);

NancyJ
07-15-2007, 11:34 AM
echo out your sql so you can see what query it is running.

tomyknoker
07-15-2007, 11:37 AM
It just gave me Resource id #4 Is that normal?

NancyJ
07-15-2007, 12:07 PM
no.
What were you trying to echo?
You should be echoing out the sql query not the result resource id.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum