2012-07-07 23 views
0

MySQL的語法錯誤,我試圖建立與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> 

謝謝您的幫助

+0

這將被濫用到最後...... – 2012-07-07 00:53:49

+0

@Paul表示您在此代碼中存在嚴重的安全問題。在向公衆提供此代碼之前,請研究SQL注入的含義。另外,你可以嘗試讓你的錯誤信息聲明如下:'die(「Anfrage fehlgeschlagen - 」。$ sql。「 - :」。mysql_error())'。這樣你會看到有問題的sql語句。 – 2012-07-07 00:58:05

回答

2

您的代碼容易受到SQL注入,和你的問題只是一個提示,爲什麼。

我們經常使用的規則是:「從用戶代理切勿輕信數據」(即考慮$ _GET或$ _POST作爲潛在的問題或更糟糕的東西)。至少,我們應該使用mysqli_real_escape_string或其他更健壯的DB框架來避免這些值。

0

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

0

無論SQL注入漏洞的,好像你發送的查詢是太長MySQL來處理。

您可以嘗試通過更改一些配置來解決此問題:嘗試和提高你的MySQL的配置文件中的參數「max_allowed_pa​​cket的」。例如:

[mysqld] 
max_allowed_packet = 64M 

這將其設置爲64MB,這意味着你將被允許發行時間最長的單個查詢爲64MB,最長的單排,你將能夠從查詢到獵犬的大小64MB 。

0
<?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> 
相關問題