gpt4 book ai didi

php - 该值未插入到数据库中

转载 作者:行者123 更新时间:2023-11-29 09:18:06 26 4
gpt4 key购买 nike

我想在数据库中存储超过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>

最佳答案

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

mysql_real_escape_string($_POST['discription'])

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

mysql_real_escape_string — Escapes special characters in a string for use in an SQL statement

您的代码应如下所示:

$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());

请注意,在末尾添加或die(mysql_error(),这会告诉您查询本身是否存在任何错误。

关于php - 该值未插入到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3421925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com