Jump to content

MySql empty Search Field


richiejones24

Recommended Posts

Can anyone tell me the best way to handle empty search field on a mysql Query? The query below if $name is left blank but the other fields are filled it shows no results, currently i use if.... else.... but that means i have a lot of code replicated, is there a better way?

 

$sql = "SELECT private.username,
se.age,
se.ro,
se.suc,
se.bu,
se.fu,
com.in,
com.ol,
se.di,
se.bo,
se.uin
FROM Reg_Profile_public AS se
INNER JOIN Reg_Profile_Private AS private
USING (uin)
INNER JOIN Reg_Profile_public_Com AS com
USING (uin)
WHERE se.age BETWEEN '$low' AND '$high' AND se.ro=''$name";

Link to comment
Share on other sites

If I have multiple parameters for my search clause I typically include logic for each clause and them implode them at the end. I usually use checks on the submitted values and not the variables that are set from those submitted values. Since you don't show where those variable are set I'll just use the variables.

$whereParts = array();
if(!empty($low) && !empty($high))
{   //Both low and high have values
    $whereParts[] = "se.age BETWEEN '$low' AND '$high'";
}
elseif(!empty($low))
{   //Only low has a value
    $whereParts[] = "se.age >= '$low'";
}
elseif(!empty($high))
{   //Only highhas a value
    $whereParts[] = "se.age <= '$high'";
}

if(!empty($name))
{
    $whereParts[] = "se.ro='$name'";
}

//Create the base query
$sql = "SELECT private.username, se.age, se.ro, se.suc, se.bu,
               se.fu, com.in, com.ol, se.di, se.bo, se.uin
        FROM Reg_Profile_public AS se
        INNER JOIN Reg_Profile_Private AS private
          USING (uin)
        INNER JOIN Reg_Profile_public_Com AS com
          USING (uin)";

//Add the WHERE conditions
if(count($whereParts)>0)
{
    $sql .= "WHERE " . implode(' AND ', $whereParts);
}

Link to comment
Share on other sites

I like mjdamato's method as that will only query the selected or populated items from the form.

 

I usually like to insert default values if none were inserted, if I want all the results to be displayed.

 

 

But take this simple example checking if all the fields of the form have a value.

 

<html>
<body>

<form action="" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
City: <input type="text" name="city" />
<input type="submit" />
</form>
<?php

if(isset($_POST)){
foreach($_POST as $post_data){
if(empty($post_data)){
echo "You have not completely filled out the form";
EXIT;
}

}
print_r($_POST);
}

?>
</body>
</html> 

Link to comment
Share on other sites

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.