我遇到这个问题,我试图在我的数据库中插入评论,但它不起作用。当我手动将值放入数据库时,显示已存在注释的选项有效。我究竟做错了什么?表格中的列'评论'是用户名,我根据评论的文件路径从另一个表中获取的film_id,以及它提交的日期。
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div class="commentdiv">
<form method="post" action="">
Comment:<br>
<textarea name='message' id='message'></textarea><br/>
<br>
<input type="submit" name="comment" value="Comment">
<br>
</form>
</div>
<?php
date_default_timezone_set('America/Curacao');
$db = new PDO('mysql:host=localhost;dbname=dbase', 'username', 'password');
if(isset($_POST['comment'])){
if(empty($_POST['message'])){
echo "There's no message";
} else {
if(isset($_SESSION['loggeduser'])){
$message = $_POST['message'];
$datum = date('YmdHis');
$username = $_SESSION['loggeduser'][0];
$path = $_SERVER['REQUEST_URI'][0];
try {
$zoekfilm = $db->prepare("SELECT film_id FROM Reviews WHERE path = :path");
$zoekfilm->bindParam("path", $path);
$zoekfilm->execute();
$film = $zoekfilm->fetch();
} catch(PDOException $b){
die("Error!: " . $b->getMessage());
}
try{
$addcomment = $db->prepare("INSERT INTO comments(Usernames, film_id, comments, date) VALUES (:username, :id , :comment, :datum )");
$addcomment->bindParam("username", $username);
$addcomment->bindParam("id", $film_id);
$addcomment->bindParam("comment", $message);
$addcomment->bindParam("datum", $datum);
$addcomment->execute();
} catch(PDOException $c){
die("Error!: " . $c->getMessage());
}
} else {
header("Location: signin.php");
}
}
}
try {
$showcomments = $db->prepare("SELECT * FROM comments ORDER BY date");
$showcomments->execute();
while($result = $showcomments->fetch(PDO::FETCH_ASSOC)){
echo '<div class="comment" style="border: 2px solid black;">';
echo '<p>'.$result['Usernames'].' </p>';
echo '<p> '.$result['comments'].'</p>';
echo '<p> Posted:'. $result['date'] .'</p>';
echo '</div>';
}
} catch(PDOException $a){
die("Error!: " . $a->getMessage());
}
?>
</body>
</html>
答案 0 :(得分:0)
我假设选择用于获取文件ID,但是当您在插入中使用该值时,您将把fetch的结果放入id中。结果是结果中的所有字段的数组(尽管在这种情况下为1),因此您需要选择要使用的字段。
M