获取MySQL数据单击时WHERE列等于HTML链接

获取MySQL数据单击时WHERE列等于HTML链接

问题描述:

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.

不太了解如何使用这个,任何帮助将不胜感激。 因此,我正在尝试显示与已单击的链接列匹配的产品。 单击按钮(搜索)时,我已成功完成搜索功能。 现在我正试图在按下链接时实现类似的方法。 p>

  $ sql =“SELECT * FROM products”; 
 if(isset($ _ POST ['Submit'])){
 if(empty($ _ POST [' 搜索'])){
 $ error = true; 
} else {
 $ searchq = mysql_real_escape_string($ _ POST ['Search']); 
 $ searchq = preg_replace(“#[^ 0-9a-z  ] #i“,”“,$ searchq); 
 $ sql。=”WHERE类型LIKE'%$ searchq%'或名称LIKE'%$ searchq%'“;  
} 
} $ query = mysql_query($ sql)或die(mysql_error()); 
  code>  pre> 
 
 

链接是这样的,所以当用户 点击链接后,它会根据名称=“”在链接中显示数据,该数据与表格产品中的列类型相匹配。 p>

 &lt; ul&gt; 
&lt; li name  =“Books”class =“menu-781”&gt;&lt; a href =“#”&gt; Books&lt; / a&gt;&lt; / li&gt; 
&lt; li name =“Perfume”class =“menu-780”  &gt;&lt; a href =“#”&gt; Perfume&lt; / a&gt;&lt; / li&gt; 
&lt; li name =“Gifts”class =“menu-789”&gt;&lt; a href =“#”&gt  ;礼物&lt; / a&gt;&lt; / li&gt; 
&lt; li name =“文具”class =“menu-778 last”“&gt;&lt; a href =”#“&gt;文具&lt; / a&gt;&lt; /  li&gt; 
&lt; / ul&gt; 
  code>  pre> 
 
 

提前致谢。 p> div>

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());

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

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