我在使用php和mysql的网站上工作,并且从数据库行生成网页网址时遇到一些问题。
我只有3页connection.php(mysql连接)index.php(其中show al products / contents缩略图带有带产品详细信息URL的按钮)和details.php,我想要显示单个产品的信息。
来自index.php的我添加一个链接以重定向到details.php页面:
<a href="details.php?id=<?php echo $row['ID']; ?>"
它的工作但是大问题在于detail.php,因为脚本不显示单个产品的详细信息,但显示所有产品,请有人可以帮助我吗?谢谢
index.php 代码
......other html code......
<div class="row">
<?php
require_once 'connection.php';
$query = "SELECT * FROM campi_name";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<div class="col-sm-4 col-md-3">
<div class="thumbnail">
<img src="<?php echo $row['Thumbnail']; ?>" alt="<?php echo $row['Title']; ? >">
<div class="caption">
<h4><?php echo substr($row['Title'], 0, 30); ?></h4>
<p><?php echo $row['Brand']; ?></p>
<?php echo $row['ID']; ?>
<p><a href="#" class="btn btn-primary btn-lg" role="button">Cofronta</a> <a href="dettagli.php?id=<?php echo $row['ID']; ?>" class="btn btn-default btn-lg" role="button">Dettagli</a></p>
</div>
</div>
</div>
<?php
}
?>
......other html code......
connection.php 代码
$DBhost = "localhost";
$DBuser = "root";
$DBpass = "";
$DBname = "prodotti";
try {
$DBcon = new PDO("mysql:host=$DBhost;dbname=$DBname",$DBuser,$DBpass);
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $ex){
die($ex->getMessage());
}
?>
details.php 代码
......other html code......
<div class="container">
<div class="row">
<?php
require_once 'connection.php';
$query = "SELECT * FROM campi_name";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<div class="col-sm-4 stylerow">
<a href="<?php echo $row['AffiliateLink']; ?>" class="thumbnail">
<img src="<?php echo $row['Thumbnail']; ?>" alt="<?php echo $row['Title']; ? >">
</a>
</div>
<div class="col-sm-8 stylerow">
<h2><?php echo $row['Title']; ?></h2>
<p><?php echo $row['Brand']; ?></p>
<button type="button" class="btn btn-primary btn-lg">Amazon</button>
</div>
</div>
</div><!-- /.container -->
......other html code......
答案 0 :(得分:3)
添加 $ ID = $ _ GET [ 'ID'];
编辑代码中的以下行
$query = "SELECT * FROM campi_name";
到
$query = "SELECT * FROM campi_name where id="'.$id.'" ";