我的处理程序中有一个表单:
<form action="../submitcomment.php" method="post">
<input maxlength=100 size=60 type="text" name="IP" value="' . $ip . '" readonly="readonly" hidden="hidden">
<input maxlength=100 size=60 type="text" name="BlogId" value="' . $blogId . '" readonly="readonly" hidden="hidden">
<input maxlength=100 size=60 type="text" name="Date" value="' . $date . '" readonly="readonly" hidden="hidden">
<input maxlength=100 size=60 type="text" name="Name" placeholder="Enter Your Name">
<input maxlength=100 size=60 type="text" name="Email" placeholder="Enter Your Email">
<input maxlength=100 size=60 type="text" name="Comment" placeholder="Enter Your Comment">
<br>
<input type="submit" name="Submit" value="Submit Your Comment">
</form>
行动是submitcomment.php
:
$ip = $_POST['IP'];
$BlogId = $_POST['BlogId'];
$Date = $_POST['Date'];
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Comment = $_POST['Comment'];
$blog = new Blogs();
if (isset($_POST['Submit']))
{
$addComment = $blog->insertComment($ip, $BlogId, $Date, $Name, $Email, $Comment);
header('Location: http://www.ryan.archi.dev.netsite.co.uk/Blog?success=1');
}else{
header('Location: http://www.ryan.archi.dev.netsite.co.uk/Blog?fail=1');
}
引用了我班上的一个函数:
function insertComment($ip, $BlogId, $Date, $Name, $Email, $Comment)
{
$query = "INSERT INTO BlogComments (Name, Comment, IPAddress, Email, BlogId, Date) VALUES ('$Name', '$Comment', '$ip', '$Email', '$BlogId', '$Date')";
$oDatabase = new database;
$connection = $oDatabase->Connect();
$result = mysql_query ($query, $connection);
return $result;
}
尝试插入不会返回或引发任何错误。 据我所知,这应该有效,你能发现我做错了吗?
答案 0 :(得分:3)
问题在于名为Date
的列 - date
是一个保留字(我猜所有已知的RDBMS)。
你必须在你的查询中转义这个词:
INSERT INTO BlogComments (Name, Comment, IPAddress, Email, BlogId, `Date`) VALUES ('$Name', '$Comment', '$ip', '$Email', '$BlogId', '$Date')
此外,您的代码为任何人提供了进行SQL注入攻击的机会,因此您至少应该逃避任何用户输入或更好地使用MySQLi或PDO。
你可以通过php函数http://php.net/mysql_real_escape_string进行转义:
$ip = mysql_real_escape_string($_POST['IP']);
$BlogId = mysql_real_escape_string($_POST['BlogId']);
$Date = mysql_real_escape_string($_POST['Date']);
$Name = mysql_real_escape_string($_POST['Name']);
$Email = mysql_real_escape_string($_POST['Email']);
$Comment = mysql_real_escape_string($_POST['Comment']);
答案 1 :(得分:0)
你调试了吗?是否已成功建立与数据库的连接?你没有mysql转义,也许插入查询失败。