2012-05-21 132 views
0

我有我的處理形式:MySQL查詢不插入

<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

UFF,讓SQL注入長壽! – shadyyx

+0

是否有任何由'mysql_query'返回的錯誤? – Sirko

+0

試圖插入返回或引發任何錯誤? – shadyyx

回答

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']); 
+0

不幸的是,這沒有工作 – RSM

+0

@RyanMurphy它給出了什麼錯誤? – shadyyx

+0

當我完成'mysqld_real_escape_string'時,它會引發此錯誤:'無法通過套接字連接到本地MySQL服務器'/tmp/mysql.sock'(2)' – RSM

0

你調試了嗎?與數據庫的連接是否成功建立?你沒有mysql轉義,也許插入查詢失敗。