Click here to Skip to main content
15,916,949 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


What I have tried:

product.php
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>
                        
                    </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">
                                        
                                        
                                            <button type="button" id="minus" class="btn btn-default btn-flat btn-lg"></button>
                                        
                                        <input type="text" name="quantity" id="quantity" class="form-control input-lg" value="1">
                                        
                                            <button type="button" id="add" class="btn btn-default btn-flat btn-lg">^__i class="fa fa-plus">
                                            </button>
                                        
                                        <input type="hidden" value="<?php echo $product['prodid']; ?>" name="id">
                                    </div>
                                    <button type="submit" class="btn btn-primary btn-lg btn-flat"> Add to Cart</button>
                                </div>
                            </form>
                        </div>
                        <div class="col-sm-6">
                            <h1 class="page-header"><?php echo $product['prodname']; ?></h1>
                            <h3>$ <?php echo number_format($product['price'], 2); ?></h3>
                            <p>Category: <a href="category.php?category=<?php echo $product['cat_slug']; ?>"><?php echo $product['catname']; ?></a></p>
                            <p>Description:</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();
?>
Posted
Updated 13-Jul-21 6:14am

1 solution

If your comments are meant to be attached to different products then you need to have some indication on the comments table to indicate which product a comment is relevant for.

Consider adding a product_id column to your comments table, and then when you run the select in the comment_show.php you need to select comments where the product_id matches the one being viewed.

Similarly when you insert the comment into the database, you'll need to make sure that you insert the correct product ID into the new column.
 
Share this answer
 
v2
Comments
Yakov Cohen 13-Jul-21 15:12pm    
Thanks you.
Here is a new code =
open();

$pro = $product['prodid'];

$sql = "SELECT * FROM comments INNER JOIN products on comments.product_id=products.id WHERE product_id=:id";
$stmt = $conn->prepare($sql);
$stmt->execute(['id'=>$pro]);

foreach($stmt as $row){
echo '

Posted by : '.$row['firstname'].'


'.$row['comment'].'';
}
$pdo->close();
?>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900