Using URL parameters in MYSQL search

前端 未结 4 1465
广开言路
广开言路 2021-01-26 08:03

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,pric

相关标签:
4条回答
  • 2021-01-26 08:08

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

    $color = $_GET['color'];
    $category = $_GET['cat'];
    
    0 讨论(0)
  • 2021-01-26 08:14

    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

    0 讨论(0)
  • 2021-01-26 08:16

    You can get it with $_GET like:

    $product_id = $_GET['product_id'];
    
    0 讨论(0)
  • 2021-01-26 08:17

    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."'";
    
    0 讨论(0)
提交回复
热议问题