gpt4 book ai didi

php - 用户评论未使用 PDO 输入数据库

转载 作者:行者123 更新时间:2023-11-29 01:55:46 24 4
gpt4 key购买 nike

我正在尝试创建一个页面以允许用户对图书发表评论。我正在检查字段是否为空,用户名是否存在于用户表中,图书 ID 是否存在于图书表中。如果所有这些都通过了,那么我想将评论输入评论表。

目前,当我填写表单并提交时,我没有得到任何输出,没有错误,只有一个空白屏幕。

代码如下:

require_once __DIR__.'/config.php';
session_start();
$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_USERNAME, DB_USERNAME, DB_PASSWORD);

$book = $_POST["book_id"];
$username = $_POST["user"];
$review = $_POST["review"];
$rating = $_POST["rating"];

if (empty($book) || empty($username) || empty($review) || empty($rating) ) {
$error = "Complete all fields";
}

if ($rating > 5 || $rating < 0) {
$error = "Please enter a rating between 1 and 5";
}

if (!isset($error)){
//no error
$stmt = $dbh->prepare("SELECT username FROM users WHERE username = :user");
$stmt->bindParam(':user', $username);
$stmt->execute();
} else {
$error = "user does not exsist";
}

if (!isset($error)){
//no error
$stmt = $dbh->prepare("SELECT b_id FROM users WHERE b_id = :book_id");
$stmt->bindParam(':book_id', $book);
$stmt->execute();
} else {
$error = "Book does not exsist, please enter another ID";
}

if (!isset($error)) {
$sql = 'INSERT INTO reviews (book_id ,username, review, rating) VALUES (:book_id,:user,:review,:rating)';
$query = $dbh->prepare($sql);

$query->execute(array(
':book_id' => $book,
':user' => $username,
':review' => $review,
':rating' => $rating
));
} else {
echo "error occured: ".$error;
exit();
}

我想进行检查并将信息输入数据库,谁能告诉我如何操作?

最佳答案

根据上次查询的错误输出,reviews 表似乎没有 username 字段。这也是合乎逻辑的 - 我认为它应该类似于 user_id(作为 PK 关系)。

Query 1 Error output: Array ( [0] => 00000 [1] => [2] => )
Query 2 Error output: Array ( [0] => 00000 [1] => [2] => )
Query 3 Error output: Array ( [0] => 42S22 [1] => 1054 [2] => Unknown column 'username' in 'field list' )

查询:

$sql = 'INSERT INTO reviews (book_id ,username, review, rating) VALUES (:book_id,:user,:review,:rating)';    
$query = $dbh->prepare($sql);

提到的错误是:

Unknown column 'username' in 'field list'

关于php - 用户评论未使用 PDO 输入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29768781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com