Reputation: 95
I'm working on a website with php and mysql and have some problems to generate web pages URL from database rows.
I have only 3 page connection.php (mysql connection) index.php (where show al products/contents thumbnails with button with product details URL) and details.php where i want show info for single product.
from index.php i add a link to redirect to details.php page with this:
<a href="details.php?id=<?php echo $row['ID']; ?>"
it's work but Big problem is in details.php because the script don't show a single products details, but show all products, please someone can help me? Thank you
index.php code
......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 code
$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 code
......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......
Upvotes: 1
Views: 306
Reputation: 644
Add $id=$_GET['id'];
edit following line in your code
$query = "SELECT * FROM campi_name";
To
$query = "SELECT * FROM campi_name where id="'.$id.'" ";
Upvotes: 3