Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';t插入到数据库的链接_Php_Mysql_Insert - Fatal编程技术网

Php Can';t插入到数据库的链接

Php Can';t插入到数据库的链接,php,mysql,insert,Php,Mysql,Insert,我有以下代码: if (!empty($_POST['w1']) && !empty($_POST['l1'])) { $str = "<a href='".$_POST['l1']."'>".$_POST['w1']."</a>"; $content = str_replace($_POST['w1'],$str, $content); } $content = nl2br($content);

我有以下代码:

    if (!empty($_POST['w1']) && !empty($_POST['l1']))   {
        $str = "<a href='".$_POST['l1']."'>".$_POST['w1']."</a>";
        $content = str_replace($_POST['w1'],$str, $content);
    }
    $content = nl2br($content);
    echo "נוסף";
    echo $content;
mysql_query("INSERT INTO `posts` VALUES ('','".$_POST['subject']."','$content','0')");
if(!empty($_POST['w1'])和&!empty($_POST['l1'])){
$str=”“;
$content=str_replace($_POST['w1'],$str,$content);
}
$content=nl2br($content);
回声“נוסף”;
echo$内容;
mysql_查询(“插入到'posts'值(''''''.'$'u POST['subject'].','$content','0')”;
php页面不会返回任何错误。但是数据没有插入到数据库中。
有人知道问题出在哪里吗?

您在查询中为$content添加了不需要的单引号

mysql_query("INSERT INTO `posts` VALUES ('','".$_POST['subject']."','$content','0')");
换成

mysql_query("INSERT INTO `posts` VALUES ('','".$_POST['subject']."','".$content."','0')");

您在查询中为$content添加了不需要的单引号

mysql_query("INSERT INTO `posts` VALUES ('','".$_POST['subject']."','$content','0')");
换成

mysql_query("INSERT INTO `posts` VALUES ('','".$_POST['subject']."','".$content."','0')");

如果抛出任何mysql错误,它将不会显示给您。执行以下操作以查看错误:

mysql_query("INSERT INTO `posts` VALUES ('','". mysql_real_escape_string($_POST['subject']) . "','" . mysql_real_escape_string($content) . "','0')") or die mysql_error();

注意SQL注入

如果抛出任何mysql错误,它将不会显示给您。执行以下操作以查看错误:

mysql_query("INSERT INTO `posts` VALUES ('','". mysql_real_escape_string($_POST['subject']) . "','" . mysql_real_escape_string($content) . "','0')") or die mysql_error();

注意SQL注入

PHP解析器不返回任何错误,因此错误可能在数据库端。 我脑子里想不出的一件事是更换

INSERT INTO `posts` VALUES ('','".$_POST['subject']."','$content','0')


因为您的数据库配置可能不允许您尝试使用的构造。

PHP解析器不会返回任何错误,因此错误可能在数据库端。 我脑子里想不出的一件事是更换

INSERT INTO `posts` VALUES ('','".$_POST['subject']."','$content','0')


因为您的数据库配置可能不允许您尝试使用的构造。

我只是认为错误报告已关闭,查询在我看来是错误的。mysql_查询(“插入
posts
value(“”,“$\u POST['subject']”,“,{$content},'0');错误报告已打开。我检查了几次查询,没有发现任何错误。要显示MySQL错误,请使用
MySQL\u error()
MySQL\u errno()
。我添加了它。它没有返回任何错误。我只是认为错误报告已关闭,查询在我看来是错误的。mysql_query(“INSERT-INTO
posts
VALUES(“”,“$_POST['subject'].”,“{$content},'0');错误报告已打开。我检查了几次查询,没有发现任何错误。要显示MySQL错误,请使用
MySQL\u error()
MySQL\u errno()
。我添加了它。它没有返回任何错误。实际上,在字符串中使用变量不是一种好的编码方式,但它是允许的,所以这不会是问题。实际上,在字符串中使用变量不是一种好的编码方式,但它是允许的,所以这不会是问题。谢谢!一旦我添加了
mysql\u real\u escape\u strin()
它就解决了我的问题。我真的不知道为什么。它没有打印错误。它只是起作用了。你知道可能的原因吗?如果你在用户输入中有像“foobar”这样的字符,它们会破坏你的SQL语句,因为像[userinput:foobar]“VALUES('foobar')”这样的东西会变成[userinput:foobar]“VALUES('foobar')”,这会导致语法错误。另外注意:这种行为至少很烦人,但也可能被滥用来进行所谓的SQL注入。这是一个巨大的安全风险,所以你应该告诉自己:谢谢!一旦我添加了
mysql\u real\u escape\u strin()
它就解决了我的问题。我真的不知道为什么。它没有打印错误。它只是起作用了。你知道可能的原因吗?如果你在用户输入中有像“foobar”这样的字符,它们会破坏你的SQL语句,因为像[userinput:foobar]“VALUES('foobar')”这样的东西会变成[userinput:foobar]“VALUES('foobar')”,这会导致语法错误。另外注意:这种行为至少很烦人,但也可能被滥用来进行所谓的SQL注入。这是一个巨大的安全风险,因此您可能应该告知自己: