2012-01-19 84 views
1

的原因不明。當我要求這個功能很奇怪,內部服務器錯誤

//Add the content to the database 
function articleadd() { 
    mysql_query("INSERT INTO site_content (title,content,reference,author,userid,authorip,day,month,year,token) VALUES ('$title','$articlebody','$reference','$author','$userid','$ipaddress','$day','$month','$year','$token')"); 
} 

對於它會產生一個內部服務器錯誤一些未知的原因。老實說,我不知道爲什麼,我要求低於這個功能,它工作正常

//Add a message if the user is not logged in 
function message() { 
mysql_query("INSERT INTO messages (from,to,subject,content,type,ip) VALUES ('$userid','$userid','The content $title is pending completion.','A article was attempted to be saved, however when the we tried to saved it there was no-one logged in. As a result we saved the article but quarantined it so it will not show on the website. To correct this error, please click the link below, review the article and confirm it should display on the site. <a href=$url>$url</a>','$type','$ipaddress')"); 

} 

與這兩個我請求他們一前一後如下

articleadd() 
    message() 

我有,在它執行功能不能用一個非常簡單的方法代替所有的mysql查詢

echo "hello world"; 

但我得到的錯誤。

我試着複製和粘貼東西,即使是「功能」一詞,以確保我不犯一個愚蠢的錯誤。不幸的是我沒有訪問與我的託管服務提供商的服務器日誌。最後,我知道我沒有傳遞變量,這是一個單獨的問題,但是基於變量和「hello world」測試的工作原理,這不是問題。

+1

我得到擔心,當我看到PHP函數的變量的值代碼不使用[PHP Prepared Statements](http://php.net/manual/en/pdo.prepared-statements.php)來防止[SQL Injection](http://en.wikipedia.org/wiki/SQL_injection)漏洞。我希望你在這裏沒有粘貼的代碼中清理你的變量。如果不是,請考慮重新編寫代碼以使用PDO Prepared Statements,而不是試圖清理變量。 – sarnold 2012-01-19 00:09:03

+0

一個簡單的['phpinfo()'](http://php.net/manual/en/function.phpinfo.php)腳本是否正確執行? – sarnold 2012-01-19 00:09:57

+1

如果'$ title'是'你不是逃避你的SQL'? **不要使用** mysql_query'函數。按照@sarnold的建議使用PDO。 – tadman 2012-01-19 00:11:56

回答

0

正如我所看到的,函數內部使用的變量沒有值。 您需要設置如$用戶ID,$作家,$標題從參數或全局變量

function articleadd($title,$articlebody,$reference,$author,$userid,$ipaddress,$day,$month,$year,$token) { 
mysql_query("INSERT INTO site_content (title,content,reference,author,userid,authorip,day,month,year,token) VALUES ('$title','$articlebody','$reference','$author','$userid','$ipaddress','$day','$month','$year','$token')");} 

而且這樣調用

articleadd('title','articlebody','reference','author','userid','ipaddress','day','month','year','token');