Jump to content

Archived

This topic is now archived and is closed to further replies.

bigkev1983

Select statement in search results page

Recommended Posts

Hello there

I'm pretty new to PHP and mianly use Dreamweaver to come up with most of the code for search interfaces to be honest so I'm hoping you guys can help me.

I have this select statement for my search results page:
$query_directory = sprintf("SELECT * FROM directory WHERE Name LIKE '%".$_POST['name']."%' ORDER BY id DESC", $colname_directory);

to get all records from the database whose name contains characters typed in by the user from the search form.

However, it doesn't work and SQL just comes back with an error. The standard statement (below) DOES return results but obviously it doesn't do what I need it to as it forces the user to type in the whole name exactly:

$query_directory = sprintf("SELECT * FROM directory WHERE Name = '".$_POST['name']."' ORDER BY id DESC", $colname_directory);

Can anyone help me make the first statement work?

Kev

Share this post


Link to post
Share on other sites
sometimes I'll replace the spaces with percents...

$query_directory = sprintf("SELECT * FROM directory WHERE Name = '".str_replace(" ","%",$_POST['name'])."' ORDER BY id DESC", $colname_directory);

Share this post


Link to post
Share on other sites
Thanks for replying.
When I tried this, I didn't get an error message this time but unfortunately I don't get any results either.

Share this post


Link to post
Share on other sites

×

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.