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

Link to comment
Share on other sites

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.

 

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.