0

我的处理程序中有一个表格:

<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;
    }

插入尝试不会返回或引发任何错误。据我所知,这应该有效,你能发现我做错了什么吗?

4

2 回答 2

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']);
于 2012-05-21T09:27:16.903 回答
0

你调试了吗?与数据库的连接是否成功建立?您没有进行 mysql 转义,可能插入查询失败。

于 2012-05-21T09:25:50.060 回答