Search string

Posted on 2004-10-27
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
    LVL 3

    Accepted 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.
    LVL 2

    Expert Comment

    check this

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

    Author Comment

    many thanks - that worked a treat.

    LVL 3

    Expert Comment

    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)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    SQL injection vulnerabilities have been described as one of the most serious threats for Web applications. Web applications that are vulnerable to SQL injection may allow an attacker to gain complete access to their underlying databases. Because…
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    856 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now