我的代码显示-1 Record Inserted错误,而不是将字段插入数据库。有什么想法为什么这样做?
<?php
include("dbconnect.php");
$con=new dbconnect();
$con->connect();
error_reporting(E_ALL);
if($_POST) {
$users_name = $_POST['name'];
$users_email = $_POST['email'];
$users_website = $_POST['website'];
$users_comment = $_POST['content'];
$users_name = htmlspecialchars($users_name);
$users_email = htmlspecialchars($users_email);
$users_website = htmlspecialchars($users_website);
$users_comment = htmlspecialchars($users_comment);
$postid = $_GET['id'];
$sSql = "INSERT INTO comments
( post_id, name, email, website,content)
VALUES ($postid, '$users_name',
'$users_email', '$users_website', '$users_comment' )";
mysql_query($sSql);
$update=mysql_affected_rows();
echo "<h2>$update Record Inserted</h2><br />";
echo '<h2> Your Comment is submitted</h2><br />';
}
?>
由于某种原因,评论表没有得到更新。我是mySQL和PHP编程的新手。任何建议对我都有很大的帮助。感谢。
答案 0 :(得分:0)
-1表示查询返回错误。
将此sql查询放入sql浏览器的sql'querier'中,看看错误是什么:
INSERT INTO
comments
(post_id,
name,
email,
website,
content)
VALUES
(2,
'name',
'email@',
'http://',
'comment')
答案 1 :(得分:0)
首先在php SQL语句中插入虚拟值&amp;注释mysql_query语句。
$sSql = "INSERT INTO comments (post_id,name,email,website,content) VALUES (100, 'anoop.pete','anoop.pete@gmail.com', 'www.anooppete.com', 'Nice Website' )";
//mysql_query($sSql);
//$update=mysql_affected_rows();
打印SQL语句......
print($sSql);
从Web浏览器复制SQL语句,在MySql中执行$ sSql
如果插入了行,则在MySQL中取消注释并再次运行同一页面。
mysql_query($sSql);
$update=mysql_affected_rows();
如果它运行,请尝试删除htmlspecialchars()
$users_name = $_POST['name'];
$users_email = $_POST['email'];
$users_website = $_POST['website'];
$users_comment = $_POST['content'];
我猜你的htmlspecialchars()正在返回一些无效字符......