您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在#1;'附近使用正确的语法。在第1行

时间:2014-12-10 09:15:27

标签: sql

我真的无法弄清楚我做错了什么。我将值传递给数据库。它存储时会起作用。它们是上一页的复选框,仅值为1,或者在phpMyadmin中为自动0。

这是我的代码:

/* SQL COMMANDS */
$mysqli = new mysqli("localhost", "root", "****" , "buffetkeuze");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } echo 'Connected... ' . mysqli_get_host_info($mysqli) . "\n";  

$insert = mysqli_query($mysqli, "INSERT INTO `buffetkeuze` ( `Gourmand` , `Party` , `Polder` ,       `Frites` , `Gebakken_aardappels` , `Rijst` , `Gegratineerde_aardappels` , `Pasta` , `Ardennerham` , `Kipsate` , `Varkenssate` , `Spareribs` , `KipPiriPiri` , `Gehaktballetjes` , `Beenham` , `Minischnitzels` , `Gerooktezalm` , `Haringplateau` , `Zalmfilet` , `Kibbeling` , `Quiche` ,  `Tempuragroenten` , `Rundercarpaccio` , `Wildpate` , `Ossenhaaspuntjes` , `Ribeye` , `Blacktiger` , `ganzenborst` , `Wildragout`  , `Fazantenfilet`  , `kaasplateau`  , `geitenkaas` , `paling` , `wildezalm` , `Oesters` , `Kreeft` , `Tonijn` , `Zeetongfilet` , `Reebiefstuk` , `Hertencarpaccio` , `gerookteeendenborst` , `garnalensalade` ) 
VALUES (  '$_POST[Gourmand]','$_POST[Party]','$_POST[Polder]','$_POST[Frites]','$_POST[Gebakken_aardappels]','$   _POST[Rijst]' ,'$_POST[Gegratineerde_aardappels]','$_POST[Pasta]','$_POST[Ardennerham]','$_POST[Kipsate]','$_POST[Varkenssate]','$_POST[Spareribs]','$_POST[KipPiriPiri]','$_POST[Gehaktballetjes]','$_POST[Beenham]','$_POST[Minischnitzels]','$_POST[garnalensalade]','$_POST[Gerooktezalm]','$_POST[Haringplateau]','$_POST[Zalmfilet]','$_POST[Kibbeling]','$_POST[Quiche]','$_POST[Tempuragroenten]','$_POST[Rundercarpaccio]','$_POST[Wildpate]','$_POST[Ossenhaaspuntjes]','$_POST[Ribeye]','$_POST[Blacktiger]','$_POST[ganzenborst]','$_POST[Wildragout]','$_POST[Fazantenfilet]','$_POST[kaasplateau]','$_POST[geitenkaas]','$_POST[paling]','$_POST[wildezalm]','$_POST[Oesters]','$_POST[Kreeft]','$_POST[Tonijn]','$_POST[Zeetongfilet]','$_POST[Reebiefstuk]','$_POST[Hertencarpaccio]','$_POST[gerookteeendenborst]' ) " );

if (!mysqli_query($mysqli, $insert)) { die('Error: ' . mysqli_error($mysqli)); } echo "values inserted " . "\n"; 

$sql =  mysqli_query($mysqli,"SELECT * FROM `buffetkeuze` ") ;

if (!mysqli_query($mysqli, $sql)) { die('Error: ' . mysqli_error($mysqli)); } echo "values selected " . "\n"; while ($row = mysqli_fetch_assoc($mysqli,$sql)) {
$Gourmand = $row['Gourmand'];
$Party = $row['Party'];
$Polder = $row['Polder'];
$Frites = $row['Frites'];
$Gebakkenaardappels = $row['Gebakken_aardappels'];
$Rijst = $row['Rijst'];
$Gegratineerdeaardappels = $row['Gegratineerde_aardappels'];
$Pasta = $row['Pasta'];

etc.    

这个错误是什么?

3 个答案:

答案 0 :(得分:0)

尝试使用双引词后变量并单引号引用您的查询,如下所示:

mysqli_query($mysqli, "INSERT INTO `buffetkeuze` (`Gourmand`, `Party`, ...) 
VALUES ('$_POST["Gourmand"]', '$_POST["Party"]', ...)" );

答案 1 :(得分:0)

您需要在帖子变量中添加引号,如下所示:

$_POST['Gourmand'],...

//在你的sql中使用这个:

$insert = mysqli_query($mysqli, "INSERT INTO `buffetkeuze` ( `Gourmand` , `Party` , `Polder` ,       `Frites` , `Gebakken_aardappels` , `Rijst` , `Gegratineerde_aardappels` , `Pasta` , `Ardennerham` , `Kipsate` , `Varkenssate` , `Spareribs` , `KipPiriPiri` , `Gehaktballetjes` , `Beenham` , `Minischnitzels` , `Gerooktezalm` , `Haringplateau` , `Zalmfilet` , `Kibbeling` , `Quiche` ,  `Tempuragroenten` , `Rundercarpaccio` , `Wildpate` , `Ossenhaaspuntjes` , `Ribeye` , `Blacktiger` , `ganzenborst` , `Wildragout`  , `Fazantenfilet`  , `kaasplateau`  , `geitenkaas` , `paling` , `wildezalm` , `Oesters` , `Kreeft` , `Tonijn` , `Zeetongfilet` , `Reebiefstuk` , `Hertencarpaccio` , `gerookteeendenborst` , `garnalensalade` )
        VALUES (  "$_POST['Gourmand']","$_POST['Party']",..... ) " );

答案 2 :(得分:0)

  1. 使用prepared statements查询。这有很多原因,包括像你的预防一样的错误。您当前的代码不正确且易受攻击。
  2. 你在值部分写了类似$ _POST[Rijst]的东西,这不是正确的变量调用。
  3. 我猜你的查询中有更多不好的东西。尝试输出最终的查询字符串,看看它是什么样的。