Php 我的评论系统赢得了';不要将内容添加到我的数据库中
我正在为我网站上的每个帖子创建一个评论系统。我希望这个系统能够识别登录的用户,这样他们所要做的就是写他们的评论,代码会自动添加他们的用户id,并在添加评论的同时将评论与帖子关联起来 在我的评论表中,我有: id-int自动递增 idea_id-int(这是post id) 用户标识-int 评论-媒体文本Php 我的评论系统赢得了';不要将内容添加到我的数据库中,php,mysql,forms,Php,Mysql,Forms,我正在为我网站上的每个帖子创建一个评论系统。我希望这个系统能够识别登录的用户,这样他们所要做的就是写他们的评论,代码会自动添加他们的用户id,并在添加评论的同时将评论与帖子关联起来 在我的评论表中,我有: id-int自动递增 idea_id-int(这是post id) 用户标识-int 评论-媒体文本 我有一个基本的web表单: <?php include 'comment.php'; ?> <form method = 'Post'> C
我有一个基本的web表单:
<?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>";
}
?>
从您的代码中,我认为您的问题在于您尚未连接到MySQL-如果您已连接但尚未包含MySQL,请尝试编写
mysql_query("QUERYHERE") or die(mysql_error());
让我们知道你的错误
如果需要连接,您需要的功能是:
mysql_connect("host", "user", "password");
mysql_select_db("database");
试试这个
<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='your file'
并使用此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>";
}
?>
为想法添加一个隐藏的表单字段\u i您应该在表单中添加一个带有帖子ID的隐藏字段。您应该阅读sql注入。注释是否包含”
?建议它们不再得到维护。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO,。您从何处获得创意?在表单中,尝试将其保存为隐藏字段,其值可以通过post方法在comment.php中获得。还可以像下面这样使用sql=“插入注释(idea_id,user_id,comment)值(“.$idea_id.”,“.$user_id.”,“.$comment.”),“id-int自动递增/idea_id-int/user_id-int/comment-mediumtext,有效!老兄,你最好!!!!!!谢谢你一直支持我。我很困惑,我很高兴我们能够解决这个问题。你建议我怎么做才能将想法添加到这个表格中?评论时间是一个很好的选择。这些想法基本上都是帖子。我只想包括那些评论的id,所以当我显示评论时,它们只是与该post相关联的评论。我可以使用post函数来获取idea\u id吗?