2012-07-07 68 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>