Php 长输入文本时出现MySQL语法错误

Php 长输入文本时出现MySQL语法错误,php,mysql,syntax-error,Php,Mysql,Syntax Error,我正在尝试用MySQL构建一个PHP表单。问题是,如果我试图在字段中添加一些长文本,每次都会出现错误 错误 您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 靠近……在1号线 生成查询的PHP代码如下所示: <?php if ( $_GET['aktion'] == "speichern" ) { $title = $_GET['title']; $description = $_GET['description'

我正在尝试用MySQL构建一个PHP表单。问题是,如果我试图在字段中添加一些长文本,每次都会出现错误

错误

您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 靠近……在1号线

生成查询的PHP代码如下所示:

<?php

if ( $_GET['aktion'] == "speichern" )
{
    $title          = $_GET['title'];
    $description    = $_GET['description'];
    $applepart      = $_GET['applepart'];
    $partnumber     = $_GET['partnumber'];
    $productcode    = $_GET['productcode'];
    $compatibility  = $_GET['compatibility'];
    $url_bild       = $_GET['url_bild'];
    $price          = $_GET['price'];

    $sql  = "INSERT INTO adressbuch ";
    $sql .= " SET ";
    $sql .= " title         = '$title', ";
    $sql .= " description   = '$description', ";
    $sql .= " applepart     = '$applepart', ";
    $sql .= " partnumber    = '$partnumber', ";
    $sql .= " productcode   = '$productcode', ";
    $sql .= " compatibility = '$compatibility', ";
    $sql .= " url_bild      = '$url_bild', ";
    $sql .= " price         = '$price' ";

    require_once ('konfiguration.php');
    $db_erg = mysql_query($sql)
        or die("Anfrage fehlgeschlagen: " . mysql_error());

    echo '<h1>Adresse wurde speichert</h1>';
    echo '<a href="auflistung.php">Auflistung anzeigen</a>';
    exit;
}
?>

<form name="" action="" method="GET" enctype="text/html">
<p>Title:<br />
<input type="text" name="title" value="" size="60" />
</p>
<p>description:<br />
<input type="text" name="description" value="" size="60" />
</p>
<p>applepart:<br />
<input type="text" name="applepart" value="" size="60" />
</p>
<p>partnumber:<br />
<input type="text" name="partnumber" value="" size="60" />
</p>
<p>productcode:<br />
<input type="text" name="productcode" value="" size="60" />
</p>
<p>compatibility:<br />
<input type="text" name="compatibility" value="" size="60" />
</p>
<p>Bild:<br />
<input type="text" name="url_bild" value="" size="60" />
</p>
<p>price:<br />
<input type="text" name="price" value="" size="60" />
</p>

<input type="hidden" name="aktion" value="speichern" />

<input type="Submit" name="" value="speichern" />
</form>

您的问题是,当您有足够长的输入时,它在某个地方有一个单引号或换行符。您不能像这样简单地连接用户输入并期望它工作。更糟糕的是,您对SQL注入攻击非常开放。找到使用框架构建SQL查询的正确方法。

您的代码容易受到SQL注入的影响,而您的问题只是一个关于原因的提示


我们一直使用的规则是:“永远不要信任用户代理的数据”(即考虑$GET或$POST中的任何可能的问题或更坏)。至少,我们应该始终使用数据库框架或更健壮的数据库框架来转义这些值。

不管SQL注入漏洞如何,似乎您发送的查询太长,MySQL无法处理

您可以尝试通过更改一些配置来克服这个问题:尝试在MySQL的配置文件中增加参数“max_allowed_packet”。例如:

[mysqld]
max_allowed_packet = 64M

这会将其设置为64MB,这意味着您可以发出的最长单个查询为64MB,您可以从查询中检索的最长单个行的大小为64MB。

这将被滥用到无穷无尽的地步…@Paul意味着您在这段代码中存在严重的安全问题。在向公众提供此代码之前,请先研究SQL注入的含义。另外,您可以尝试让错误消息语句这样说:
die(“Anfrage fehlgeschlagen-->$sql-->:“.mysql\u error())
。这样,您将看到有问题的sql语句。
<?php
      require_once ('konfiguration.php');

      if(isset($_POST['title']))
      {
        $title = mysql_real_escape_string(htmlspecialchars($_POST['title']));
        $description = mysql_real_escape_string(htmlspecialchars($_POST['description']));
        $applepart = mysql_real_escape_string(htmlspecialchars($_POST['applepart']));
        $partnumber = mysql_real_escape_string(htmlspecialchars($_POST['partnumber']));
        $productcode = mysql_real_escape_string(htmlspecialchars($_POST['productcode']));
        $compatibility = mysql_real_escape_string(htmlspecialchars($_POST['compatibility']));
        $url_bild = mysql_real_escape_string(htmlspecialchars($_POST['url_bild']));
        $price = mysql_real_escape_string(htmlspecialchars($_POST['price']));
        $insert = mysql_query("INSERT INTO `adressbuch` (`title`,`description`,`applepart`,`partnumber`,`productcode`,`compatibility`,`url_bild`,`price`) VALUES ('$title','$description','$applepart','$partnumber','$productcode','$compatibility','$url_bild','$price')");
        if (!$insert)
        {
          die('Eintrag konnte nicht gespeichert werden: ' . mysql_error());
        }
      }

    ?>

    <form method="POST" action="?page= ">
      <span>Neuer G&auml;stebucheintrag verfassen:</span> <br />
      <span>Title</span><input type="text" name="title" /> <br />
      <span>Description</span><textarea cols="16" rows="5"  name="description"></textarea> <br />
      <span>Apple Part</span><input type="text" name="applepart" /> <br />
      <span>Part Number</span><input type="text" name="partnumber" /> <br />
      <span>Product Code</span><input type="text" name="productcode" /> <br />
      <span>Compatibility</span><input type="text" name="compatibility" /> <br />
      <span>Image</span><input type="text" name="url_bild" /> <br />
      <span>Price</span><input type="text" name="price" /> <br />
      <input type="submit" value="Speichern"/> <br />
    </form>