Page 1 of 1

Database search

Posted: Fri May 02, 2008 2:45 pm
by Zavin
I am trying to write a script that will do a search of my database and return the results. The problem I am having is that I have multiple fields that can be searched. If the field is left blank I want the script to ignore that field and only search the field that the user enters information into. The is for a game and below is part of the code I am using. It returns information from the database just not exactly the way it is suppose to. Can some one take a look at it and tell me what I'm doing wrong?

This field should return all users equal to or below the inputed level.

Code: Select all

if($_POST['level']!="")
        
if($_POST['level'] <= $user_search->level);
This field Should return all users equal to or above the inputed level.

Code: Select all

if($_POST['level2']!="")
        
if($_POST['level2'] >= $user_search->level);
This field should return all users with equal to or more than the inputed money.

Code: Select all

if($_POST['money']!="");
        
if($_POST['money'] <= $user_search->money);

Re: Database search

Posted: Fri May 02, 2008 2:56 pm
by Verminox
What do you want to happen if the user enters information into two fields?

1. If you want to make sure that only one field is entered, I'd rather suggest having only one text field, and 3 submit buttons each with a different value.

Example:

Code: Select all

<input type="text" name="search" />
<button type="submit" name="submit" value="level">Level Less Than Search</button>
<button type="submit" name="submit" value="level2">Level Greater Than Search</button>
<button type="submit" name="submit" value="money">Money Search</button>
Then you can check which button was pressed:

Code: Select all

<?php
if($_POST['search'] != "")
{
    if($_POST['submit'] == 'level')
    {
        // code for level less than
    }
    else if($_POST['submit'] == 'level2')
    {
        // code for level greater than
    }
    else if($_POST['submit'] == 'money')
    {
        // code for money
    }
}
?>
If you don't want one text box and multiple buttons, you can also have 3 different forms, each with its own hidden input indicating what that form was meant to do. That way, you avoid having to decide where to go based on which text box was full and which text box was empty.


2. If you instead you want to mix search results if multiple fields are entered either by OR-ing the conditions or AND-ing them, then the approach is completely different.

Re: Database search

Posted: Fri May 02, 2008 3:10 pm
by Kastor
Try something like this

Code: Select all

$where_query = "";
 
if($_POST['level']!="") {
    $where_query .= "{$_POST['level']} <= user.level AND ";
}
 
if($_POST['level2']!="") {
    $where_query .= "{$_POST['level2']} >= user.level AND ";
}
 
if($_POST['money']!="") {
    $where_query .= "{$_POST['money']} >= user.money AND ";
}
 
if ($where_query != "") $where_query = substr($where_query, 0, strlen($where_query) - 5);
 
$mysql_result = mysql_query("SELECT * FROM users_table AS user ".($where_query != "" ? "WHERE {$where_query}" : ""));

Re: Database search

Posted: Fri May 02, 2008 4:15 pm
by Zavin
What do you want to happen if the user enters information into two fields?
I want the results to match the fields. If a user inputs minimum level and maximum level then the results should only be between those 2 inputs. The same goes for if they input a minimum and and money amount the results should be at and above that level AND at or above that money amount.

Verminox
That didn't work. It gave me the same results as my code. Which is every user no matter what field you input.