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? 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 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"; 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
Archived
This topic is now archived and is closed to further replies.