Jump to content

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


Edited by Barand
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.