我正在为我网站上的每个帖子创建一个评论系统。我希望这个系统能够识别登录的用户,这样他们所要做的就是写下他们的评论,代码会自动添加他们的user_id,并将评论与帖子相关联,同时添加评论。
在我的评论表中,我有:
id - int auto increment
idea_id - int(这是帖子ID)
user_id - int
评论 - 中等文本
我有一个基本的网络表单:
<?php
include 'comment.php';
?>
<form method = 'Post'>
Comment: <br/>
<input type = 'text' name = 'comment' id = 'comment' autocomplete= 'off' />
<input type = 'submit' name = 'submit' value = 'Comment' />
</form>
然后comment.php是:
<?php
if(isset($_POST['submit'])) {
$comment = $_POST['comment'];
$user_id = $_SESSION['user_id'];
if(empty($comment)) {
$message = "You Haven't Written Anything";
} else {
if(isset($_GET['user_id'])) {
$_SESSION['user_id'] = $_GET['user_id'];
}
mysql_query("INSERT INTO comments VALUES('', '".$user_id."', '".$comment."') ");
$message = "OK! Thanks for leaving your comment!";
}
echo "<div class = 'box'>$message</div>";
}
?>
现在我只添加了user_id和评论,因为我试图解决问题。我也不知道如何获取代码来识别帖子。非常感谢能帮助我的人。
答案 0 :(得分:0)
从您的代码中,我认为您的问题是您没有连接到MySQL - 如果您已连接但尚未包含它,请尝试编写
mysql_query("QUERYHERE") or die(mysql_error());
告诉我们您的错误。
如果您需要连接,您需要的功能是:
mysql_connect("host", "user", "password");
mysql_select_db("database");
答案 1 :(得分:0)
试试这个
<form method ='Post' Action='comment.php' name='comment_form' >
Comment: <br/>
<input type ='text' name='comment' value='' id='comment' autocomplete= 'off' />
<input type ='submit' name ='submit' value = 'Comment' />
</form>
在此代码中,您忘记了value ='',而action ='您的文件'
并使用此PHP代码
<?php
session_start() ;
if(isset($_POST['submit'])) {
$comment = mysql_real_escape_string($_POST['comment']);
$user_id = $_SESSION['user_id'];
if(empty($comment)) {
echo "You Haven't Written Anything";
} else {
mysql_query("INSERT INTO comments ( idea_id,user_id,comment) VALUES('', '".$user_id."', '".$comment."') ") or die(mysql_error());
echo "OK! Thanks for leaving your comment!";
if(isset($_GET['user_id'])) {
$_SESSION['user_id'] = $_GET['user_id']; /// this is wrong here , user can enter manually id via link and you will let him be in session ???
}
}
//echo "<div class = 'box'>$message</div>";
}
?>