I’m creating a comment system on my website, each product page has its own comments.
The problem is that when I create a message it appears on all product pages.
How to make every message that was written under a certain product displayed on the same product?
I think what needs to be created new column page_id
in comments
table, but how to do that?
In products
table I have an id
column for each product, if that helps.
product.php
<?php include 'includes/session.php'; ?>
<?php
$conn = $pdo->open();
$slug = $_GET['product'];
try{
$stmt = $conn->prepare("SELECT *, products.name AS prodname, category.name AS catname, products.id AS prodid FROM products LEFT JOIN category ON category.id=products.category_id WHERE slug = :slug");
$stmt->execute(['slug' => $slug]);
$product = $stmt->fetch();
}
catch(PDOException $e){
echo "There is some problem in connection: " . $e->getMessage();
}
//page view
$now = date('Y-m-d');
if($product['date_view'] == $now){
$stmt = $conn->prepare("UPDATE products SET counter=counter+1 WHERE id=:id");
$stmt->execute(['id'=>$product['prodid']]);
}
else{
$stmt = $conn->prepare("UPDATE products SET counter=1, date_view=:now WHERE id=:id");
$stmt->execute(['id'=>$product['prodid'], 'now'=>$now]);
}
?>
<?php include 'includes/header.php'; ?>
<body class="hold-transition skin-blue layout-top-nav">
<div class="wrapper">
<?php include 'includes/navbar.php'; ?>
<div class="content-wrapper">
<div class="container">
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-sm-9">
<div class="callout" id="callout" style="display:none">
<button type="button" class="close"><span aria-hidden="true">×</span></button>
<span class="message"></span>
</div>
<div class="row">
<div class="col-sm-6">
<img src="<?php echo (!empty($product['photo'])) ? 'images/'.$product['photo'] : 'images/noimage.jpg'; ?>" width="100%" class="zoom" data-magnify-src="images/large-<?php echo $product['photo']; ?>">
<br><br>
<form class="form-inline" id="productForm">
<div class="form-group">
<div class="input-group col-sm-5">
<span class="input-group-btn">
<button type="button" id="minus" class="btn btn-default btn-flat btn-lg"><i class="fa fa-minus"></i></button>
</span>
<input type="text" name="quantity" id="quantity" class="form-control input-lg" value="1">
<span class="input-group-btn">
<button type="button" id="add" class="btn btn-default btn-flat btn-lg"><i class="fa fa-plus"></i>
</button>
</span>
<input type="hidden" value="<?php echo $product['prodid']; ?>" name="id">
</div>
<button type="submit" class="btn btn-primary btn-lg btn-flat"><i class="fa fa-shopping-cart"></i> Add to Cart</button>
</div>
</form>
</div>
<div class="col-sm-6">
<h1 class="page-header"><?php echo $product['prodname']; ?></h1>
<h3><b>$ <?php echo number_format($product['price'], 2); ?></b></h3>
<p><b>Category:</b> <a href="category.php?category=<?php echo $product['cat_slug']; ?>"><?php echo $product['catname']; ?></a></p>
<p><b>Description:</b></p>
<p><?php echo $product['description']; ?></p>
</div>
</div>
<br>
<?php include 'comment.php'; ?>
<?php include 'comment_show.php'; ?>
</div>
<div class="col-sm-3">
<?php include 'includes/sidebar.php'; ?>
</div>
</div>
</section>
</div>
</div>
<?php $pdo->close(); ?>
<?php include 'includes/footer.php'; ?>
</div>
<?php include 'includes/scripts.php'; ?>
<script>
$(function(){
$('#add').click(function(e){
e.preventDefault();
var quantity = $('#quantity').val();
quantity++;
$('#quantity').val(quantity);
});
$('#minus').click(function(e){
e.preventDefault();
var quantity = $('#quantity').val();
if(quantity > 1){
quantity--;
}
$('#quantity').val(quantity);
});
});
</script>
</body>
</html>
comment.php
<form action="" method="post">
<div class="form-group">
<textarea class="form-control" style="resize: none;" name="comment" id="" cols="30" rows="5" placeholder="Enter Message" required></textarea>
<button type="sumbit" name="sumbitComment" class="btn btn-primary pull-right">Send Message</button>
</div>
</form>
<?php
if(isset($_POST['sumbitComment'])){
if(isset($_SESSION['user'])){
$conn = $pdo->open();
$comment = $_POST['comment'];
$firstName = $user['firstname'];
$userID = $user['id'];
$sql = "INSERT INTO comments (comment, user_id, firstname) VALUES (:comment, :user_id, :firstname)";
$stmt = $conn->prepare($sql);
$stmt->execute(['comment'=>$comment, 'user_id'=>$userID, 'firstname'=>$firstName]);
$pdo->close();
}
}
?>
comment_show.php
<?php
$conn = $pdo->open();
$sql = "SELECT * FROM comments";
$stmt = $conn->prepare($sql);
$stmt->execute();
foreach($stmt as $row){
echo '<h3>Posted by : '.$row['firstname'].'</h3><br>
<p>'.$row['comment'].'</p>';
}
$pdo->close();
?>