chet139 Posted February 12, 2008 Share Posted February 12, 2008 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 More sharing options...
chet139 Posted February 12, 2008 Author Share Posted February 12, 2008 Hi, If what i have stated is unclear please ask.....Im new to it so I need some help....I think its simple to solve..... Just posting again just incase it gets burried. Link to comment https://forums.phpfreaks.com/topic/90665-simple-multivariable-search/#findComment-464782 Share on other sites More sharing options...
trq Posted February 12, 2008 Share Posted February 12, 2008 Try using empty() instead of isset(). eg; if(!empty($_POST['custEmail'])) Link to comment https://forums.phpfreaks.com/topic/90665-simple-multivariable-search/#findComment-464785 Share on other sites More sharing options...
chet139 Posted February 12, 2008 Author Share Posted February 12, 2008 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 https://forums.phpfreaks.com/topic/90665-simple-multivariable-search/#findComment-464800 Share on other sites More sharing options...
GameYin Posted February 12, 2008 Share Posted February 12, 2008 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 https://forums.phpfreaks.com/topic/90665-simple-multivariable-search/#findComment-464854 Share on other sites More sharing options...
chet139 Posted February 12, 2008 Author Share Posted February 12, 2008 you dont have do that. But I have it working now thanks. I used an array with what I already had. Thanks for everyone who gave a positive input. Link to comment https://forums.phpfreaks.com/topic/90665-simple-multivariable-search/#findComment-465176 Share on other sites More sharing options...
GameYin Posted February 13, 2008 Share Posted February 13, 2008 If it's solved, then click "Topic Solved." Link to comment https://forums.phpfreaks.com/topic/90665-simple-multivariable-search/#findComment-465742 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.