On 24-Oct-2003, hokieghal99 <hokiegal99@hotmail.com> wrote:
[color=blue]
> I'd like to get user input from an html form into a mysql select
> statement. Here's where I'm stumped:
>
> $result = mysql_query("SELECT * FROM dept WHERE notes LIKE
> '%search-string%'",$db);
>
> I need to get the user's input into the '%search-string%' section, but I
> do not understand how to do this. I can hard-code a specific search
> string and it will work, but I want the users to be able to dynamically
> define the search-string. So, I created a basic html form and used the
> post method to grab their input, but now I can't insert that input into
> the mysql select statement. Any ideas? I think it should be easy, I just
> don't know how to do it. I've tried this:
>
> $result = mysql_query("SELECT * FROM dept WHERE notes LIKE
> '%$_POST["search"]%'",$db);
>
> But it doesn't work. Below is the form in html and the php file:[/color]
remove the double quotes from around search.
$result = mysql_query("SELECT * FROM dept WHERE notes LIKE
'%$_POST[search]%' ",$db);
Please note that this is VERY UNSAFE and leaves you open to a security
problem called an SQL Injection attack. At the very least you should code:
$search_string = addslashes($_POST['search']);
$result = mysql_query("SELECT * FROM dept WHERE notes LIKE
'%$search_string%' ",$db);
--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to
jamesbutler@willglen.net (it's reserved for spammers)