该值未插入db

时间:2010-08-06 07:55:50

标签: php mysql phpmyadmin

我想在数据库中存储1000多个字符,我使用MySQL数据库。

当我插入50-100个字符时,代码成功运行。但是当我插入1000个字符时,它没有被插入。它也没有出错。

当我在phpMyAdmin中使用类似的插入查询时,查询成功运行。 这是查询,前两个字段是varchar&最后一个字段是Longtext:

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. ")

这在phpMyAdmin中成功运行

但是当我尝试在PHP中运行它时,它就无法运行。

这是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>

1 个答案:

答案 0 :(得分:2)

在字符串变量之前使用mysql_real_escape_string函数,例如:

mysql_real_escape_string($_POST['discription'])

这很可能是因为文本包含应该转义的单引号。

  

mysql_real_escape_string - Escapes   字符串中的特殊字符供使用   在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(),这会告诉您查询本身是否有任何错误。