当调用我的函数时,会出现MySQL语法错误,有什么想法吗?
您的SQL语法有错误;查看与MySQL服务器版本相对应的手册,了解在第1行"附近使用的正确语法
function getComments1() {
$query = mysql_query("SELECT * FROM comments") or die(mysql_error());
while($post = mysql_fetch_assoc($query)) {
echo $post['Author'];
}
}
function addComment($cName, $cContent) {
$query = mysql_query("INSERT INTO comments VALUES(null,'$cName','$cContent'") or die(mysql_error());
}
<?php
include('includes/functions.php');
mysql_set_charset ( "utf8" );
if(isset($_POST['submit'])) {
if(isset($_POST['CommentName'])) {
if(isset($_POST['CommentContent'])) {
addComment($_POST['CommentName'],$_POST['CommentContent']);
header("Location: derger.php");
} else { "text missing";
}
} else {
echo "name missing";
include('herger.php');
}
} else {
header("Location: werger.php");
}
?>
("INSERT INTO comments VALUES(null,'$cName','$cContent'")
您没有关闭值。应该是:
("INSERT INTO comments VALUES(null,'$cName','$cContent')")
此外,正如这里的每个人都会提醒您的那样,mysql_函数是不推荐使用的。您应该使用PDO或MySQLi。一个简单的谷歌搜索任何一个都会给你大量的资源过渡到。
错误在于插入查询中缺少右括号。更新查询如下:
$query = mysql_query("INSERT INTO comments VALUES(null,'$cName','$cContent')") or die(mysql_error());
注意'$cContent'
后面的右括号。
其他值得注意的问题是,您仍然使用不推荐使用的mysql_
扩展,您应该开始使用mysqli或pdo。还请注意,您当前的实现容易受到SQL注入攻击,您至少应该实现mysql_real_eescape_string。请看一下https://www.owasp.org/index.php/SQL_Injection