Rickshaw
Rickshaw

Reputation: 107

Get MySQL data WHERE column equals HTML link when clicked

Don't quite know how to work this one, any help would be much appreciated. So I'm trying to display the products which match the column of the link which have been clicked. I have succeeded in doing it for the search function when I click a button (Search). Now I'm trying to implement similar method for when a link is pressed.

    $sql = "SELECT * FROM products ";
    if(isset($_POST['Submit'])){
        if(empty($_POST['Search'])){
            $error = true;
        }else{
        $searchq = mysql_real_escape_string($_POST['Search']);
        $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
        $sql .= "WHERE type LIKE '%$searchq%' or name LIKE '%$searchq%'";       
    } 
} $query = mysql_query($sql) or die(mysql_error());

The links are something like this, so when a user has clicked the link, it displays data according to name="" in link which matches the column type in the table products.

<ul>
            <li name="Books" class="menu-781"><a href="#">Books</a></li>
            <li name="Perfume" class="menu-780"><a href="#">Perfume</a></li>
            <li name="Gifts" class="menu-789"><a href="#">Gifts</a></li>
            <li name="Stationery" class="menu-778 last""><a href="#">Stationery</a></li>
        </ul>

Thanks in advance.

Upvotes: 2

Views: 113

Answers (3)

moh kaw
moh kaw

Reputation: 43

if your link will go to another page or to same page you can send a parameter with link the code will be like

 <li name="Books" class="menu-781"><a href="somepage.php?name='Books'">Books</a></li>

and in the other page you can get the value by using :

 $searchq=$_GET['name'];

now you got the value from the link not from the form search and you can do the queries

Upvotes: 2

3y3skill3r
3y3skill3r

Reputation: 1026

Well, you can create GET request instead of POST. For example, you can create url like this:

http://someurl.com/?filter=gifts

After you'll need to grab variable from GET, not from POST.

One advice, your application will be more safe if you will use PDO. For more informations, visit: http://hu1.php.net/manual/en/book.pdo.php

Upvotes: 0

zzlalani
zzlalani

Reputation: 24354

try it this code

<ul>   
        <li name="Books" class="menu-781"><a href="search.php?q=Books">Books</a></li>
        <li name="Perfume" class="menu-780"><a href="search.php?q=Perfume">Perfume</a></li>
        <li name="Gifts" class="menu-789"><a href="search.php?q=Gifts">Gifts</a></li>
        <li name="Stationery" class="menu-778 last""><a href="search.php?q=Stationery">Stationery</a></li>
    </ul>

and in your php

$sql = "SELECT * FROM products ";
    if(isset($_GET['q'])){
        if(empty($_GET['q'])){
            $error = true;
        }else{
            $searchq = mysql_real_escape_string($_GET['q']);
            $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
            $sql .= "WHERE type LIKE '%$searchq%' or name LIKE '%$searchq%'";       
        } 
    } 
$query = mysql_query($sql) or die(mysql_error());

Upvotes: 2

Related Questions