簡體   English   中英

長輸入文本時出現MySQL語法錯誤

[英]MySQL Syntax Error at long entered text

我正在嘗試使用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注入的攻擊,而您的問題只是提示原因。

我們一直使用的規則是:“永遠不要信任來自用戶代理的數據”(即將$ _GET或$ _POST中的任何內容視為可能存在問題或更糟糕)。 至少,我們應該始終使用mysqli_real_escape_string或更強大的DB框架來轉義這些值。

你的問題是當你有足夠長的輸入時,它在某個地方有一個單引號或換行符。 你不能簡單地連接像這樣的用戶輸入並期望它工作。 更糟糕的是,您對SQL注入攻擊持開放態度。 找到使用框架構建SQL查詢的正確方法。

無論SQL注入漏洞如何,似乎您發送的查詢對於MySQL來說太長了。

您可以嘗試通過更改某些配置來克服這個問題:嘗試在MySQL的配置文件中引發參數“max_allowed_pa​​cket”。 例如:

[mysqld]
max_allowed_packet = 64M

這將設置為64MB,這意味着您將被允許發出的最長單個查詢是64MB,並且您將能夠從查詢中檢索的最長單行大小為64MB。

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM