Jump to content

Simple multivariable search


chet139

Recommended Posts

All,

 

Firstly can I just say hello and introduce myself here. Im obviously new to this forum (looks like a great proactive place!) but also quite a newbie to the PHP&MySQL environment.

 

So, Im in the process of creating a search form(which is done) see code below:

 

<form action="getCust.php" method="post">

Customer ID:<br /> <input type="text" name="custId" /><br />

Customer Title:<br />

<select name="custTitle">

<option value="">Select Title</option>

<option value="Dr">Dr</option>

<option value="Prof">Prof</option>

</select><br />

Customer Firstname:<br /> <input type="text" name="custFirstname" /><br />

Customer Surname:<br /> <input type="text" name="custSurname" /><br />

Customer Email:<br /> <input type="text" name="custEmail" /><br />

<br />

<input type="submit" value="Search" />

<input type="reset" name="reset" value="Clear Fields"/>

</form>

 

 

 

This then goes to my processing page which you can see if getCust.php this is where I need some help I cant just simply use ANDs and ORs in the WHERE clause, because the user is not forced to enter all the fields to search on -and I dont want them to. So I have used and IF ISSET line for each of the VARS but the problem is the query processes all of them anyway redering the query useless see below for what I have done....I think I am close. thanks.

 

require_once ('dbcon.html');//connect to db

 

 

$query = "select custId, custTitle, custFirstname, custSurname, custAddress, custEmail, custPhone, custFax

from customer

where ";

 

if(isset($_POST['custId'])) $query .= "custId = '" . $_POST['custId'] . "' ";

if(isset($_POST['custTitle'])) $query .= "and custTitle = '" . $_POST['custTitle'] . "' ";

if(isset($_POST['custFirstname'])) $query .= "and custFirstname = '" . $_POST['custFirstname'] . "' ";

if(isset($_POST['custSurname'])) $query .= "and custSurname = '" . $_POST['custSurname'] . "' ";

if(isset($_POST['custEmail'])) $query .= "and custEmail = '" . $_POST['custEmail'] . "' ";

 

 

Link to comment
https://forums.phpfreaks.com/topic/90665-simple-multivariable-search/
Share on other sites

Hey thanks for the reply.

 

Thats gave a better response however if you look in the code of the if statement there is a AND which is included in the string....any ideas of a work around for this? cos otherwise if its just one VAR being passed though its reading "WHERE and custId = '3'...." - which is not right

Have you even created the $_POST variables?? Doesn't look like it to me from what I saw from your code

 

<?php
$custID = $_POST['custID'];
$custTitle = $_POST['custTitle'];
$custFirstname = $_POST['custFirstname'];
$custSurname = $_POST['custSurname'];
$custEmail = $_POST['custEmail'];
?>

 

Your variables are now set.

 

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.