Forum Moderators: open

Message Too Old, No Replies

search form

search by first letter, any letter

         

gargoyle

9:04 am on Jan 31, 2008 (gmt 0)

10+ Year Member



Hi ya all!
I'm new in this forum and in need of help (like that's new around coders forums). English isn't my first language so my texts can look pretty cryptic sometimes. Sorry about that...

This is about search form. I have Apache 2.2.4, MySQL 5.0.27, PHPMyAdmin 2.9.2 and I'm using PHP 5.2 (and HTML for making form, duh). I suppose this should be here in Database section instead of PHP.

To my problem; I'm trying to find a solution to search form working like this:
User types one (or more) letters into form field. There are several form fields by the way. But when I type for example letter 'r' I get EVERY word containing letter 'r', not just those beginning with that letter.

I have been banging my head against the wall for at least a week because of this and all I have found is something like this:

select * FROM my_table WHERE field=a%

To that kind of help I'm just going to say "Thanks a bunch but what if it starts with letter p or letter w?"

There's also form for inserting new things to database so there might be names starting with numbers as well. And if that isn't enough... I'm from Scandinavia so we have couple of letters rest of the world doesn't have so listing all alphabets and numbers in one "select" command sounds a bit idiotic.

Here is that problem part of my code, thanks to anyone who knows how to "conquer this mountain".

if (isset($_REQUEST['field1']) && isset($_REQUEST['field2']) && isset($_REQUEST['field3']) && isset($_REQUEST['field4']) && isset($_REQUEST['field5']) && isset($_REQUEST['field6']) && isset($_REQUEST['field7']) && isset($_REQUEST['field8'])) {

$results = mysql_query("select * from jasen where field1 like '%$_REQUEST[field1]%' AND field2 LIKE '%$_REQUEST[field2]%' AND field3 LIKE '%$_REQUEST[field3]%' AND field4 LIKE '%$_REQUEST[field4]%' AND field5 LIKE '%$_REQUEST[field5]%' AND field6 LIKE '%$_REQUEST[field6]%' AND field7 LIKE '%$_REQUEST[field7]%' AND field8 LIKE '%$_REQUEST[field8]%'");

while ($row = mysql_fetch_assoc($results)) {
echo "<table bgcolor=\"#ffffff\" border=1>";
echo "<tr><td>$row[field1] </td><td> $row[field2] </td><td> $row[field3] </td><td> $row[field4] </td><td> $row[field5] </td><td> $row[field6] </td><td> $row[field7] </td><td> $row[field8] </td></tr>";
echo "</table>";
}
} else {
echo "Results<P>&nbsp;</P>";
}

coopster

3:33 pm on Jan 31, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, gargoyle.

You showed the answer to your issue in your query ...

I have been banging my head against the wall for at least a week because of this and all I have found is something like this:

select * FROM my_table WHERE field=a%

but you have not yet applied it! Remove the percent sign from the beginning of your LIKE statement:

$results = mysql_query("select * from jasen where field1 like '%$_REQUEST[field1]%' AND

Also, you should be escaping those query strings. NEVER trust user supplied input.
mysql_real_escape_string [php.net]

See also:
String Comparison Functions [dev.mysql.com]

gargoyle

7:12 am on Feb 4, 2008 (gmt 0)

10+ Year Member



Wow!
Thank you so much! Finally I have it in the way it should be. And thanks about that escape string tip as well.