如何使用PHP開發簡單的商品評論和評分功能

WBOY
發布: 2023-09-21 10:34:01
原創
1124 人瀏覽過

如何使用PHP開發簡單的商品評論和評分功能

如何使用PHP開發簡單的商品評論和評分功能

PHP作為一種廣泛應用於網站開發的腳本語言,可以幫助我們開發各種功能豐富的網站。其中一個常見的功能就是商品評論和評分功能。本文將介紹如何使用PHP開發簡單的商品評論和評分功能,並提供具體的程式碼範例。

首先,我們需要在資料庫中建立一個用於儲存評論資訊的表,表結構如下:

CREATE TABLE comments (
id int(11) NOT NULL AUTO_INCREMENT,
product_id int(11) NOT NULL,
user_id int(11) NOT NULL,
comment text NOT NULL,
rating int(11) NOT NULL,
created_at datetime NOT NULL,
PRIMARY KEY (id )
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

表中的欄位包括:id(評論的唯一識別)、product_id(被評論的商品ID)、user_id(評論使用者的ID)、comment(評論內容)、rating(評分)、created_at(評論創建時間)。

接下來,我們需要建立一個用於顯示商品評論的頁面和一個用於提交評論的頁面。

  1. 顯示商品評論的頁面(comments.php):

// 連結資料庫,這裡使用PDO方式
$dbhost = 'localhost';
$dbname = 'your_database_name';
$username = 'your_username';
$password = 'your_password';

#try {

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $username, $password);
登入後複製
登入後複製

} catch (PDOException $e) {

echo "数据库连接失败: " . $e->getMessage();
exit;
登入後複製
登入後複製

}

// 查詢某個商品的註解清單
$product_id = $_GET['product_id'];
$stmt = $conn->prepare("SELECT * FROM comments WHERE product_id = :product_id");
$stmt->bindParam(':product_id', $product_id, PDO::PARAM_INT);
#INTc ->execute();
$comments = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>


    <?php foreach ($comments as $comment): ?>
    <li>
        <strong>用户:</strong> <?php echo $comment['user_id']; ?><br>
        <strong>评论:</strong> <?php echo $comment['comment']; ?><br>
        <strong>评分:</strong> <?php echo $comment['rating']; ?><br>
        <strong>时间:</strong> <?php echo $comment['created_at']; ?><br>
    </li>
    <?php endforeach; ?>
    登入後複製

    < ;/ul>

    1. 提交商品評論的頁面(submit_comment.php):

    // 連線資料庫
    $dbhost = 'localhost';
    $dbname = 'your_database_name';
    $username = 'your_username';
    $password = 'your_password';

    #try {

    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $username, $password);
    登入後複製
    登入後複製

    } catch (PDOException $e) {

    echo "数据库连接失败: " . $e->getMessage();
    exit;
    登入後複製
    登入後複製

    }

    // 取得POST過來的註解內容與評分
    $product_id = $_POST['product_id'];
    $user_id = $_POST['user_id'];
    $comment = $_POST['comment'];
    $rating = $_POST['rating'];
    $created_at = date('Y-m-d H:i :s');

    // 插入評論資料到資料庫
    $stmt = $conn->prepare("INSERT INTO comments (product_id, user_id, comment, rating, created_at) VALUES (:product_id , :user_id, :comment, :rating, :created_at)");
    $stmt->bindParam(':product_id', $product_id, PDO::PARAM_INT);
    $stmt->bindParam( ':user_id', $user_id, PDO::PARAM_INT);
    $stmt->bindParam(':comment', $comment, PDO::PARAM_STR);
    $stmt->bindParam(': rating', $rating, PDO::PARAM_INT);
    $stmt->bindParam(':created_at', $created_at, PDO::PARAM_STR);
    $stmt->execute();

    // 跳回商品評論頁
    header("Location: comments.php?product_id=" . $product_id);
    exit;
    ?>

    以上就是如何使用PHP開發簡單的商品評論和評分功能的完整程式碼範例。透過這些程式碼,我們可以實現一個簡單的商品評論系統,用戶可以在商品頁面查看其他用戶的評論,並且可以提交自己的評論和評分。

    當然,這只是一個簡單的範例,實際上還可以進一步完善和最佳化,例如增加使用者認證、評論的刪除和編輯功能等。希望以上內容可以幫助你。

    以上是如何使用PHP開發簡單的商品評論和評分功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!