m2244 Posted March 5, 2013 Share Posted March 5, 2013 Hello, I am trying to make a web based application in which a user (internal office tool only) can query one of two databases. I have heard about some sort of PHP 'controls' which might help with things like querying a DB when the form fields are empty. In other words I am providing some text input fields like 'lastname', 'e-mail', etc and I want the user to be able to enter info into as many or as few of the fields as they wish, without messing up the query. I believe these controls have some handy tools like 'Exact Phrase' radio buttons, etc. Can anyone point me in a direction with this? Quote Link to comment https://forums.phpfreaks.com/topic/275285-need-help-with-phpsql-controls/ Share on other sites More sharing options...
Barand Posted March 5, 2013 Share Posted March 5, 2013 (edited) Basically, if a value is supplied, include it in the query WHERE clause. If no value is supplied, don't. eg $where = array(); $whereclause = ''; if ($_POST['lastname']) { $where[] = sprintf("(lastname = '%s')", $mysqli->real_escape_string($_POST['lastname'])); } // ditto for other search inputs if (count($where) > 0) $whereclause = 'WHERE ' . join(' AND ', $where); $sql = "SELECT foo FROM bar $whereclause"; Edited March 5, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/275285-need-help-with-phpsql-controls/#findComment-1416806 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.