1

I am new to PHP, i am now stuck at the transferring of id selected into another page.

this is my first page, home.php

 <?php
   require 'dbfunction.php';

   $con = getDbConnect();

   if (mysqli_connect_errno($con)) {
       "Failed to connect to MySQL: " . mysqli_connect_error();
   } else {
       $result = mysqli_query($con, "SELECT * FROM category");

       while ($category = mysqli_fetch_array($result)) {
 ?>

      <div class="col-md-4">
          <a class="thumbnail" href="product.php?cat=<?php echo $category['categoryid']; ?>">
          <img src="<?php echo "img/" . $category['image'] ?>" width="200" height="200">
           <div class="title"><h3><?php echo $category['title']; ?></h3></div>
          </a>
      </div>               

<?php
       }
   mysqli_close($con);
    }
 ?> 

My second page, product.php

<?php
  require 'dbfunction.php';

    $con = getDbConnect();

     if (mysqli_connect_errno($con)) {
        "Failed to connect to MySQL: " . mysqli_connect_error();
     } else {
         $result = mysqli_query($con, "SELECT * FROM product");

            while ($product = mysqli_fetch_array($result)) {
?>

    <div class="col-md-4">       
       <a class="thumbnail" href="productInfo.php?cat=<?php echo $product['categoryid']; ?>&code=<?php echo $product['productname']; ?>">
       <img src="<?php echo "img/" . $product['productimage']; ?>"  width="250" height="220">
       <div class="title"><h3><?php echo $product['productname']; ?></h3></div>
       </a>

    </div>

<?php
      }

    }
?>

data in my database,

category

categoryid   PM2103
name         PaperModel
image        papermodel.jpg

categoryid   GP4567
name         GlossyPaper
image        GlossyPaper.jpg



product

categoryid   PM2103
productname  PaperModel
Productimage papermodel.jpg

categoryid   PM2103
productname  PaperModel222
Productimage papermodel222.jpg

categoryid   PM2103
productname  PaperModel333
Productimage papermodel333.jpg

categoryid   GP4567
productname  GlossyPaper
Productimage GlossyPaper.jpg

categoryid   GP4567
productname  GlossyPaper222
Productimage GlossyPaper222.jpg

when a person select a category in home.php, it would bring the person into another page product.php, with all the product in the category list. I'm not sure what code i should use to link them together.

j08691
  • 204,283
  • 31
  • 260
  • 272
Yee Meng
  • 21
  • 2

2 Answers2

0

Change this in product.php

$cat_id = mysqli_escape_real_string($_GET['cat']);

$result = mysqli_query($con, "SELECT * FROM product WHERE categoryid={$cat_id}");


Note: But I'd recommend to use Prepared Statements/PDO to avoid SQL injection.
Read more on SQL injection: here, here

Community
  • 1
  • 1
Parag Tyagi
  • 8,780
  • 3
  • 42
  • 47
0

Your home.php page is alright ... modify product.php

<?php
require 'dbfunction.php';

$con = getDbConnect();
$cat=$_GET['cat'];


 if (mysqli_connect_errno($con)) {
    "Failed to connect to MySQL: " . mysqli_connect_error();
 } else {
     $result = mysqli_query($con, "SELECT * FROM product where categoryid='".$cat."'");

        while ($product = mysqli_fetch_array($result)) {
?>

<div class="col-md-4">       
   <a class="thumbnail" href="productInfo.php?cat=<?php echo $product['categoryid']; ?>&code=<?php echo $product['productname']; ?>">
   <img src="<?php echo "img/" . $product['productimage']; ?>"  width="250" height="220">
   <div class="title"><h3><?php echo $product['productname']; ?></h3></div>
   </a>

</div>

<?php
  }

}
?>
Parag Tyagi
  • 8,780
  • 3
  • 42
  • 47
abh
  • 1,189
  • 2
  • 14
  • 30