Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Search string

Posted on 2004-10-27
Medium Priority
Last Modified: 2012-03-15
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


Question by:daleoran
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

KvdnBerg earned 500 total points
ID: 12420280
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.

Expert Comment

ID: 12420321
check this

$query  = "SELECT * FROM imageinfo WHERE year LIKE '%".$_GET['searchtext']."%'  LIMIT $limitvalue, $limit";
LVL 10

Author Comment

ID: 12420326
many thanks - that worked a treat.


Expert Comment

ID: 12420810
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question