Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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/mysql代码有什么问题?_Php_Mysql - Fatal编程技术网

什么';我的php/mysql代码有什么问题?

什么';我的php/mysql代码有什么问题?,php,mysql,Php,Mysql,我在玩弄一种博客。我犯了一个错误,当我说: 我的PHP如下所示: $query = "INSERT INTO articles (articleTitle, articleType, articleText, userID) VALUES('$articleTitle', '$articleType', '$articleText', '$userID')"; $result = mysql_query($query); var_dump($artic

我在玩弄一种博客。我犯了一个错误,当我说:

我的PHP如下所示:

    $query = "INSERT INTO articles (articleTitle, articleType, articleText, userID)
        VALUES('$articleTitle', '$articleType', '$articleText', '$userID')";

    $result = mysql_query($query);

    var_dump($articleTitle);
    var_dump($articleType);
    var_dump($articleText);
    var_dump($userID);

    var_dump($result);

    echo mysql_error();
我的结果是:

    string 'New News! (Sounds like noo noo's)' (length=33)
    string 'News' (length=4)
    string 'NEWS is here! This is Michael Bender reporting on the new news that we now have news! Isn't that exciting news?' (length=111)
    string '1' (length=1)
    boolean false
您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 附近的'新闻','新闻在这里!我是迈克尔·本德,正在报道 新消息在第二行


那么为什么mysql不接受我的查询呢?谢谢

您必须使用
mysql\u real\u escape\u string
对传递给mysql\u query的所有变量进行转义。此外,您可能需要使用PDO或mysqli。

您必须使用
mysql\u real\u escape\u string
对传递给mysql\u query的所有变量进行转义。此外,您可能需要使用PDO或mysqli。

以下是一些将破坏您的查询的内容:

串'新消息!(听起来像noo-noo's)’(长度=33)

单引号

在查询中使用值之前,需要先转义这些值。否则,你将成为战争的受害者

例如:

$query = "INSERT INTO articles (articleTitle, articleType, articleText, userID)
        VALUES('%s', '%s', '%s', %d)";

$query = sprintf($query,
   mysql_real_escape_string($articleTitle),
   mysql_real_escape_string($articleType), 
   mysql_real_escape_string($articleText), 
   (int) $userID); // assuming your userId is an integer

这里有一些东西会打断您的查询:

串'新消息!(听起来像noo-noo's)’(长度=33)

单引号

在查询中使用值之前,需要先转义这些值。否则,你将成为战争的受害者

例如:

$query = "INSERT INTO articles (articleTitle, articleType, articleText, userID)
        VALUES('%s', '%s', '%s', %d)";

$query = sprintf($query,
   mysql_real_escape_string($articleTitle),
   mysql_real_escape_string($articleType), 
   mysql_real_escape_string($articleText), 
   (int) $userID); // assuming your userId is an integer

在数据上使用mysql\u real\u escape\u string(),因为它可以防止查询因包含特殊字符而崩溃。

在数据上使用mysql\u real\u escape\u string(),因为它可以防止查询因包含特殊字符而崩溃。

不要使用
mysql\u*
函数族。使用
PDO
(最佳)或
mysqli*
函数(不太好)。错误会准确地告诉您错误在哪里(
s附近)
),您的错误是“正在转义字符串”。通过在“Right”之前插入\来修复它。很抱歉错过了。谢谢大家的帮助!我知道这很愚蠢。在这种情况下,只需回显您的查询并尝试在phpmyadmin或MySQL终端上直接执行即可。你很快就能得到线索。。它们不再得到维护,并且已经开始使用。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果选择PDO,。请不要使用
mysql.*
函数系列。使用
PDO
(最佳)或
mysqli*
函数(不太好)。错误会准确地告诉您错误在哪里(
s附近)
),您的错误是“正在转义字符串”。通过在“Right”之前插入\来修复它。很抱歉错过了。谢谢大家的帮助!我知道这很愚蠢。在这种情况下,只需回显您的查询并尝试在phpmyadmin或MySQL终端上直接执行即可。你很快就能得到线索。。它们不再得到维护,并且已经开始使用。看到了吗?相反,学习,并使用or-将帮助您决定哪一个。如果您选择PDO。