1

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......
2
  • 3
    Look at SELECT * FROM campi_name, you need to tell it what you want. SELECT * FROM campi_name where id = ? then bind the GET ID. $stmt->execute(array($_GET['id'])); Commented Mar 6, 2017 at 1:25
  • you will need to use the GET on details.php just like chris85 said Commented Mar 6, 2017 at 2:44

1 Answer 1

3

Add $id=$_GET['id'];

edit following line in your code

$query = "SELECT * FROM campi_name";

To

$query = "SELECT * FROM campi_name where id="'.$id.'" ";
Sign up to request clarification or add additional context in comments.

1 Comment

@Raman your code is prone to sql injection attack $id should be binded

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.