PHP/MySQL:如何在您的网站中创建评论部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1171997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:19:53  来源:igfitidea点击:

PHP/MySQL: How to create a comment section in your website

phpmysqlcomments

提问by doomspork

Instead of asking 'how to use PHP/MySQL to let users affect webpages' I'll ask this, because I learn better from projects and examples.

我不会问“如何使用 PHP/MySQL 来让用户影响网页”,我会问这个,因为我从项目和示例中学习得更好。

So how would I incorporate a VERY basic comment feature using PHP and MySQL?

那么我将如何使用 PHP 和 MySQL 合并一个非常基本的评论功能?

回答by

Create a new table called comments

创建一个名为comments的新表

They should have a column containing the id of the post they are assigned to.

他们应该有一列包含分配给他们的帖子的 ID。

Make a form which adds a new comment to that table.

制作一个向该表添加新评论的表单。

An example (not tested so may contain lil' syntax errors): I call a page with comments a post

一个例子(未经测试,因此可能包含小语法错误):我将带有评论的页面称为帖子

Post.php

后.php

<!-- Post content here -->

<!-- Then cmments below -->
<h1>Comments</h1>
<?php
$result = mysql_query("SELECT * FROM comments WHERE postid=0");
//0 should be the current post's id
while($row = mysql_fetch_object($result))
{
?>
<div class="comment">
By: <?php echo $row->author; //Or similar in your table ?>
<p>
<?php echo;$row->body; ?>
</p>
</div>
<?php
}
?>
<h1>Leave a comment:</h1>
<form action="insertcomment.php" method="post">
<!-- Here the shit they must fill out -->
<input type="hidden" name="postid" value="<?php //your posts id ?>" />
<input type="submit" />
</form>

insertcomment.php

插入评论.php

<?php
//First check if everything is filled in
if(/*some statements*/)
{
//Do a mysql_real_escape_string() to all fields

//Then insert comment
mysql_query("INSERT INTO comments VALUES ($author,$postid,$body,$etc)");
}
else
{
die("Fill out everything please. Mkay.");
}
?>

You must change the code a bit to make it work. I'n not doing your homework. Only a part of it ;)

您必须稍微更改代码才能使其工作。我不做你的功课。只是其中的一部分;)

回答by doomspork

It's a hard question to answer without more information. There are a number of things you should consider when looking at implementing commenting on an existing website.

如果没有更多信息,这是一个很难回答的问题。在现有网站上实施评论时,您应该考虑许多事项。

How will you address the issue of spam? It doesn't matter how remote your website is, spammers WILL find it and they'll filled it up in no time. You may want to look into something like reCAPTCHA (http://recaptcha.net/).

您将如何解决垃圾邮件问题?无论您的网站有多远,垃圾邮件发送者都会找到它,并且很快就会填满它。您可能想要研究类似 reCAPTCHA ( http://recaptcha.net/) 的内容。

The structure of the website may also influence how you implement your comments. Are the comments for the overall site, a particular product or page, or even another comment? You'll need to know the relationship between the content and the comment so you can properly define the relationship in the database. To put it another way, you know you want an email address, the comment, and whether it is approved or not, but now we need a way to identify what, if anything, the comment is linked to.

网站的结构也可能影响您如何实施您的评论。是针对整个网站、特定产品或页面的评论,还是其他评论?您需要知道内容和评论之间的关系,以便您可以在数据库中正确定义关系。换句话说,您知道您想要一个电子邮件地址、评论以及它是否被批准,但现在我们需要一种方法来识别评论链接到什么(如果有的话)。

If your site is already established and built on a PHP framework (CakePHPfor instance) you'll need to address how to integrate your code properly with what is already in place.

如果您的站点已经建立并构建在 PHP 框架(例如CakePHP)上,您将需要解决如何将您的代码与已有的内容正确集成。

Lastly, there are a number of resources and tutorials on the web for PHP. If you do a quick google search for something along the lines of "PHP blog tutorial" I'm sure you'll find hundreds and the majority will show you step by step how to implement comments.

最后,网络上有许多关于 PHP 的资源和教程。如果您在 Google 上快速搜索“PHP 博客教程”中的内容,我相信您会找到数百个,其中大多数将逐步向您展示如何实现评论。

回答by dassouki

Normalization is your best friend in comment/rank/vote system. Learn it. A lot of sites are now moving to PDO ... learn that as well.

规范化是您在评论/排名/投票系统中最好的朋友。学习它。很多网站现在都在转向 PDO ......也学习一下。

there are no right , right-er, or wrong (well there is) ways of doing it, you need to know the basic concepts and take them from there. IF you don't feel like learning, then perhaps invest in a framework like cakephp or zend framework, or a ready made system like wordpress or joomla.

没有正确的、正确的或错误的(确实存在)方法,您需要了解基本概念并从中吸取教训。如果你不想学习,那么也许可以投资一个像 cakephp 或 zend 框架这样的框架,或者像 wordpress 或 joomla 这样的现成系统。

I'd also recommend the book wicked cool php scriptsas it has a comment system example in it.

我还推荐这本书wicked cool php scripts因为它有一个评论系统示例。

回答by rtacconi

You can create a 'comment' table, with an id as primary key, then you add a text field to capture the text inserted by the user and you need another field to link the comment table to the article table (foreign key). Plus you need a field to store the user that has entered a comment, this field can be the user's email. Then you capture via GET or POST the user's email and comment and you insert everything in the DB:

您可以创建一个“评论”表,以 id 作为主键,然后添加一个文本字段来捕获用户插入的文本,并且您需要另一个字段将评论表链接到文章表(外键)。另外,您需要一个字段来存储输入评论的用户,该字段可以是用户的电子邮件。然后您通过 GET 或 POST 捕获用户的电子邮件和评论,并将所有内容插入数据库:

"INSERT INTO comment (comment, email, approved) VALUES ('$comment', '$email', '$approved')"

This is a first hint. Of course adding a comment feature it takes a little bit. Then you should think about a form to let the admin to approve the comments and how to publish the comments in the end of articles.

这是第一个提示。当然,添加评论功能需要一点时间。然后你应该考虑一个表单让管理员批准评论以及如何在文章末尾发布评论。

回答by Gr33N

This is my way i do comments (I think its secure):

这是我做评论的方式(我认为它是安全的):

<h1>Comment's:</h1>
<?php 
$i  = addslashes($_POST['a']);
$ip = addslashes($_POST['b']);
$a  = addslashes($_POST['c']);
$b  = addslashes($_POST['d']);
if(isset($i) & isset($ip) & isset($a) & isset($b))
{
    $r = mysql_query("SELECT COUNT(*) FROM $db.ban WHERE ip=$ip"); //Check if banned
    $r = mysql_fetch_array($r);
    if(!$r[0]) //Phew, not banned
    {
        if(mysql_query("INSERT INTO $db.com VALUES ($a, $b, $ip, $i)"))
        {
            ?>
            <script type="text/javascript">
                window.location="/index.php?id=".<?php echo $i; ?>;
            </script>
            <?php
        }
        else echo "Error, in mysql query";  
    }
    else echo "Error, You are banned.";
}
$x = mysql_query("SELECT * FROM $db.com WHERE i=$i");
while($r = mysql_fetch_object($x) echo '<div class="c">'.$r->a.'<p>'.$row->b.'</p> </div>';

?>  
<h1>Leave a comment, pl0x:</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="hidden" name="a" value="<?php $echo $_GET['id']; ?>" />
    <input type="hidden" name="b" value="<?php $echo $_SERVER['REMOTE_ADDR']; ?>" />
    <input type="text" name="c" value="Name"/></br>
    <textarea name="d">
    </textarea>
    <input type="submit" />
</form>

This does it all in one page (This is only the comments section, some configuration is needed)

这在一页中完成(这只是评论部分,需要一些配置)

回答by vitalyp

I'm working on this right now as well. You should also add a datetime of the comment. You'll need this later when you want to sort by most recent.

我现在也在做这件事。您还应该添加评论的日期时间。稍后当您想按最近排序时将需要它。

Here are some of the db fields i'm using.

这是我正在使用的一些数据库字段。

id (auto incremented)
name
email
text
datetime
approved