1

how to use url id of one page for other page in where clause like i have send http://localhost/Eshopper/products.php?id=1 where the id is for product like in my table i have tbl_products in that

product_id ,produc_name , brand_id
where the brand is select form menu linke i have menu for samsung when we click on that it will shown all samsung mobile i have use where query for getting data i was able to get the data by passing manual id like

SELECT * FROM tbl_products WHERE brand_id=1 but i want to get this brand id=1 from url that i passed through http://localhost/Eshopper/products.php?id=1 so this id 1 automacticlly added to the where condtion so to do this

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252

3 Answers3

2

You have to use $_GET.

For example, you url is id=1:

So,

<?php

/** ID is not set or it is not a integer. **/
if (!isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT))
    exit('Invalid ID.');

/** Remove other characters. **/
$iD = intval($_GET['id']);

?>

We first check if the id is set and if it is an actual number.

Then we sanitize the input to remove everything that isn't numeric.

Reading Material

isset

filter_var

intval

Script47
  • 14,230
  • 4
  • 45
  • 66
0

You can use the id on multiple pages by saving it in a session as follows.

<?php
if (!isset($_SESSION)) {
    session_start();
}

// Set $_GET id to $_SESSION id
if (isset($_GET['id'])) {
    $_SESSION['id'] = $_GET['id'];
}

// Set default value
if (!isset($_SESSION['id'])) {
    $_SESSION['id'] = 1;
}


// Now you can use $_SESSION['id'] instead of $_GET['id'];
Peter
  • 8,776
  • 6
  • 62
  • 95
0

I was able to get id by url I just unable to get that id into where condition like

$url = $_SERVER['QUERY_STRING'];
query = "SELECT * FROM `tbl_products` WHERE brand_id= '$url'";

echo $query;

so I get id=1 when I echo but when put this $query in where it was not get data

When I use static id I get data

like

$query = "select * from tbl_products where brand_id=1";

then I get list like one, two, three

But when I use

$query = "select * from tbl_products where brand_id='$url'";

I don't get any data so what is issue with this query

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459