Using URL parameters in MYSQL search

2020-05-08 08:11发布

I am trying to built a search page for products in which i have two table products and inventory, i have my product details in product and inventory details like color,size,price etc in inventory. Please suggest a solution i am really stuck and i need a better solution for a powerful design. Below are all necessary details:

Product:

product_id
product_name
product_sku
product_desc
product_category

Inventory:

inventory_id
product_id
inventory_color
inventory_size

MY QUERY:

SELECT p.product_name, p.product_sku, p.product_desc, i.inventory_color, i.inventory_color
FROM products as p
INNER JOIN  invetory as i ON p.product_id = i.product_id

URL:

www.abc.com/index.php?cat=shoe&color=black

How can i get the cat and color from URL and use them in my query? Or am i doing the right way?

4条回答
走好不送
2楼-- · 2020-05-08 08:45

You can catch values from an url. you can use " $_GET['parameter'] "

$color = $_GET['color'];
$category = $_GET['cat'];
查看更多
Summer. ? 凉城
3楼-- · 2020-05-08 08:53

using PDO, something like -

$dbh = new PDO(...)
$sth = $dbh->prepare('SELECT p.product_name, p.product_sku, p.product_desc, i.inventory_color
                      FROM products as p
                      INNER JOIN  invetory as i ON p.product_id = i.product_id 
                      WHERE p.cat = ? AND i.inventory_color = ?');
$sth->execute(array($_GET['cat'], $_GET['color']));
$results = $sth->fetchAll();

from example at http://www.php.net/manual/en/pdo.prepare.php

查看更多
手持菜刀,她持情操
4楼-- · 2020-05-08 09:00

Yes, you are in the right path. Get the values from url using get or request

$category=mysql_real_escape_string($_GET['cat']);
$color=mysql_real_escape_string($_GET['color']);

and make your SQL as

SQL="SELECT p.product_name, p.product_sku, p.product_desc, i.inventory_color, i.inventory_size
FROM products as p
INNER JOIN  invetory as i ON p.product_id = i.product_id
WHERE p.product_category='".$category."' AND i.inventory_color='".$color."'";
查看更多
混吃等死
5楼-- · 2020-05-08 09:00

You can get it with $_GET like:

$product_id = $_GET['product_id'];
查看更多
登录 后发表回答