2010-08-06 56 views
0

我想在數據庫中存儲超過1000個字符,我使用MySQL數據庫。值不插入db

當我插入50-100個字符代碼成功運行。但是,當我插入1000個字符時,它不會被插入。它也不會給出錯誤。

當我在phpMyAdmin中使用類似的插入查詢時,查詢成功運行。 這是查詢,頭兩個字段是VARCHAR &最後一個字段是LONGTEXT:在phpMyAdmin

但是,當我試圖在PHP中運行它,然後它不能運行

INSERT INTO news (headline,date,discription) 
VALUES ("hi","date","A crumbling pitch and some resurgent Indian bowling have set up a gripping deciding Test with the series lead threatening to slip out of the hosts' grasp. India had snuck ahead at the end of the third day, but were dominant by lunch on the fourth as Pragyan Ojha and Amit Mishra ran through the Sri Lankan batting. Thilan Samaraweera, the centurion from the first innings, held firm and is key to Sri Lanka's fortunes as they try to build a lead that is competitive enough to give their spinners a chance. ") 

這成功運行。

這裏是PHP代碼:

$insert = "INSERT INTO news (headline,date,discription) 
VALUES ('".$_POST['headline']."','".$_POST['date5']."','".$_POST['discription']."')"; 
$add_news = mysql_query($insert); 

&這個代碼是在標籤使用

<textarea rows="2" cols="2" style="overflow: scroll; height: 90px; width: 635px;" name="discription"></textarea> 

回答

2

使用mysql_real_escape_string功能您的字符串變量之前,例如:

mysql_real_escape_string($_POST['discription']) 

這可能很可能是因爲文本包含單引號,應該是esc模仿了。

mysql_real_escape_string - SQL語句中的轉義 特殊字符的字符串使用

您的代碼應該是這樣的:

$headline = mysql_real_escape_string($_POST['headline']); 
$description= mysql_real_escape_string($_POST['discription']); 
$date= mysql_real_escape_string($_POST['date5']); 

$insert = "INSERT INTO news (headline,date,discription) VALUES ('".$headline."','".$date."','".$description."')"; 
$add_news = mysql_query($insert) or die(mysql_error()); 

注意添加or die(mysql_error()末,這會告訴你查詢本身是否有錯誤。

+0

嘿感謝其工作 非常感謝你 – 2010-08-06 08:02:32

+0

@ Pratikg88:歡迎您... – Sarfraz 2010-08-06 08:03:48

+0

另外,還要確保你以某種方式捕捉錯誤,因爲我看到了你的查詢(斯里蘭卡)的話,如果你沒有逃脫它肯定會給出錯誤。 – Centurion 2010-08-06 08:04:17