Need a bit of help on my search bar code... Thanks :)

Hi there! I have an SQL database and a website both hosted by Godaddy. I’m using Wordpress as a CMS. I need to code a search bar to query my database. I’ve started coding a bit of a search bar and have it showing up, but not querying the database… I’d very much like to be able to search in the box and it query my database and display the table. The tables are:

Reference
FIRSTNAME
LASTNAME
ADDRESS
ADDRESS2LINE
CITY
STATE
ZIP4
ZIP
LOANAMT1
MORT1LOANDATE
MORT1LOANTYPE
LENDER1

The database name is i2369553_wp2

the table name is New Data

And here is the code ive written.

Search

Seach for: in Reference First Name Last Name Address Address2 City State Zip Loan Amount Mortgage Loan Date Loan Type/option> Lender <?php //This is only displayed if they have submitted the form if ($searching == "yes") { echo "

Results

"; //If they did not enter a search term we give them an error if ($find == "") { echo "

You forgot to enter a search term"; exit; } // Otherwise we connect to our Database mysql_connect("localhost", "databasename", "mypassword") or die(mysql_error()); mysql_select_db("New Data") or die(mysql_error()); //filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //to search for search terms, in the field the user specified $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'"); //displays the results while($result = mysql_fetch_array( $data )) { echo $result['Reference']; echo " "; echo $result['FIRSTNAME']; echo " "; echo $result['LASTNAME']; echo " "; echo $result['ADDRESS']; echo " "; } //This counts the number or results - and if there weren't any it gives them a little message explaining that $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query "; } //reminds them what they searched for echo "Searched For: " .$find; } ?>

Thanks!!

What error are you getting? You’re only showing us half of the code so it’s hard for us to know.

Also, move away from mysql_query (Google why not to use it).

Here’s a nice little query for you, it’s similar to yours but in PDO, and for added measure you can search both first name and last name columns in 1 e.g. “Russell Howard” or “RussellHoward” and it will still yield results.

I found this out when I ran into problems querying by first & last names.

[PHP]

<?php $statement = $dbconn->prepare("SELECT REPLACE(REPLACE(RTRIM(FIRSTNAME), ' ', '_'), '_', ''), REPLACE(REPLACE(RTRIM(LASTNAME), ' ', '_'), '_', ''), REPLACE(REPLACE(RTRIM(ADDRESS), ' ', '_'), '_', ''), Reference, FIRSTNAME, LASTNAME, ADDRESS FROM users WHERE (REPLACE(REPLACE(RTRIM(FIRSTNAME), ' ', '_'), '_', '') LIKE :terms) OR (REPLACE(REPLACE(RTRIM(LASTNAME), ' ', '_'), '_', '') LIKE :terms) OR (REPLACE(REPLACE(RTRIM(ADDRESS), ' ', '_'), '_', '') LIKE :terms) OR Reference LIKE :terms OR Address LIKE :terms ORDER BY users.LASTNAME DESC"); $statement->execute([ ":terms" => "%" . $find . "%" ]); $users = $statement->fetchAll(PDO::FETCH_ASSOC); if (!$users){ //No results found echo "

No results found

"; }else { foreach ($users AS $user){ //User(s) found - handle here } } ?>

[/PHP]

Sponsor our Newsletter | Privacy Policy | Terms of Service