• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

Search string

Hi All,
I have a form that contains a single text box and a submit button

<form action="ppsearch.php" method="post">
<input type="text" name="searchtext"><br>
<input type="submit" value="Search">

When this is passed over to ppsearch.php I want it to display anything that resembles the text in "searchtext"
But what is the syntax for the query (I'm a real novice at this)

$query  = "SELECT * FROM imageinfo WHERE year LIKE (WHAT DO I PUT HERE?)  LIMIT $limitvalue, $limit";

Thanks in advance


  • 2
1 Solution
Assuming that you are just looking for a value in the field year of your table, it would be:

LIKE '%{$_POST['searchtext']}%'

Here the % are wildcards (much like you use * in file systems), and the {} brackets are for giving the start and end of the variable, this is needed because the variable itself ($_POST['searchtext']) has single quotes in it and this would imply the end for the statement because the mysql value also starts and ends with single quotes. So these brackets tell php - from { to } is a variable, don't stop at the single quote. Hope this helps.
check this

$query  = "SELECT * FROM imageinfo WHERE year LIKE '%".$_GET['searchtext']."%'  LIMIT $limitvalue, $limit";
daleoranAuthor Commented:
many thanks - that worked a treat.

Just to comment on suresh_asp's suggestion: as you can see in the given example of the author, the form method = post, so _GET would not work, that's only for method = get and variables passed through urls. (BTW method = get in forms I would not recommend, should only be used if you have a good reason to)

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now