Jump to content

Need help with PHP/SQL 'controls'


m2244

Recommended Posts

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

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


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.