制作网站评论系统

时间:2012-07-09 14:11:35

标签: php javascript ajax forms comments

我和一位朋友正在尝试为我们的网站制作一个评论系统。我们有一些简单的代码可以将值插入到mysql数据库中,然后再读取并显示为注释,但是数据没有正确发送到目前为止。我们都是ajax,php和mysql的新手,所以它可能只是一个愚蠢的错误! :P

html:

<form id="postComment" action="Comments.js"  method="post"> 
<input type="email" name="email" onchange="checkEmail();" id="email" /> </br>
<div id ="emailerror">
<p id="emailerror"> </p>
</div> </br>
<input type="text" name="username" id="username" /> </br>
<input type="text" name="content" id="content" /> </br>
<input type="button" value="submit" onclick="commentUpload();" />



</form>

Javascript文件:

function commentUpload() //uploads comment to sQl table
{
var email = document.getElementById("email").value //gets the user's email
var username = document.getElementById("username").value //gets the user's username
var content = document.getElementById("content").value //gets the comment content
// var articleName = document.getElementById("articleName") gets the article name

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.open("GET","commentUpload.php?email=" + email + "&username=" +username +    "&content="+content,true);
xmlhttp.send();

}

和php:

<?php 

$email=mysql_real_escape_string($_GET['email']);
$username=mysql_real_escape_string($_GET['username']);
$content=mysql_real_escape_string($_GET['content']);


$query="INSERT INTO Comments
VALUES (1, email, username, content)";


mysql_query($query);
mysql_close();
?> 

提前感谢您的帮助!

4 个答案:

答案 0 :(得分:1)

目前,您永远不会将所需的值插入查询中。这行代码:

$query="INSERT INTO Comments VALUES (1, email, username, content)";

在某些时候,它需要接收您之前收集的变量,如下所示:

$query = "INSERT INTO Comments VALUES (1, %s, %s, %s)";
$query = sprintf($query, $email, $username, $content);

答案 1 :(得分:0)

你在PHP中的$查询是错误的。您需要将变量插入其中。如果是第一个

$query = "INSERT INTO Comments VALUES (1, '" . $email ."', 
'" . $username . "', '". $content . "')";

另外不要忘记,如果你在SQL中插入一个字符串,你需要在值周围添加'',就像我在我的例子中所做的那样。

答案 2 :(得分:-1)

你试过去“localhost / commentUpload.php”吗?你需要看到一些错误。如果是这样,你应该修复你的SQL代码。作为第一个答案说。你应该使用auto_increment作为id。

答案 3 :(得分:-1)

所以这可能是我,但我建议你在添加或上传评论时不要使用任何javascript; ajax可以提供一些非常强大的代码,但是在客户端查询数据库时使用它可能会更好,而不是在服务器端插入数据库!

HTML代码应该直接跳转到PHP,而不是像之前编程的那样通过AJAX进行转换。而不是使用一些javascript在$ _POST和$ _GET之间进行更改,您只需更改HTML中的方法。

<form id="postComment" action="comments.php" method="get">

接下来,您必须确保包含与数据库的连接。 w3Schools has a nice tutorial on establishing connections。同时,您还必须确保使用句点将从$ _GET获取的参数连接到查询字符串中。

<?php 

    $email =mysql_real_escape_string($_GET['email']);
    $username = mysql_real_escape_string($_GET['username']);
    $content = mysql_real_escape_string($_GET['content']);

    $connect = mysql_connect("server","name","password");
    if (!$con) {die('Unable to connect..!!';}
    mysql_select_db("database", $connect);

    $query = "insert into Comments values (1,'" . $email 
    ."', '" . $username . "', '". $content . "')";

    mysql_query($query);
    mysql_close();

?>

我希望有所帮助!祝其他网站好运!