[英]How to transfer the id selected on to another page for database in php?
I am new to PHP, i am now stuck at the transferring of id selected into another page. 我是PHP的新手,现在只能将选择的ID转移到另一个页面。
this is my first page, home.php 这是我的第一页,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 我的第二页,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. 当某人在home.php中选择类别时,会将其带到另一个页面product.php中,所有产品都在该类别列表中。 I'm not sure what code i should use to link them together.
我不确定应该使用什么代码将它们链接在一起。
Change this in product.php 在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 . 注意:但是我建议使用Prepared Statements / PDO以避免SQL注入 。
Read more on SQL injection: here , here 阅读更多有关SQL注入的信息: 此处 , 此处
Your home.php page is alright ... modify product.php 您的home.php页面还可以...修改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
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.