2015-10-06 65 views
2

我遇到了一个错误,我不知道我的代码有什么问题。有人能帮我吗?SQL更新时出错

Error: UPDATE users SET voornaam = Test, achternaam = Test2, mail = [email protected], tel = ,adres = , geslacht = man, bestuur = 0, tc = 0, ic = 0, jec = 0, rvr = , instructeur = 0, webmaster = 0 WHERE id = 1 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'adres = , geslacht = man, bestuur = 0, tc = 0, ic = 0, jec = 0, rvr = , instruct' at line 1

$id = $_GET['u']; 

$sql = "UPDATE users SET voornaam = $voornaam, achternaam = $achternaam, 
     mail = $mailadres, tel = $tel, adres = $adres, geslacht = $geslacht, 
     bestuur = $bestuur, tc = $tc, ic = $ic, jec = $jec, rvr = $rvr, 
     instructeur = $instructeur, webmaster = $webmaster WHERE id = ".$id.""; 

if ($conn->query($sql) === TRUE) { 
    echo "New record created successfully"; 
} else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 
+0

外貌像你没有设置'$ tel'和'$ adres'的值,它们是空的吗?为了测试,考虑在字符串上使用引号(如'voornaam ='$ voornaam'')。但实际上,应该使用prepared语句。 – FirstOne

+0

你的$ adres是空的,你的查询'adres ='给出错误,如果它们是空字符串,它应该是这样的qoutes:'adres ='$ adres'' – kakajan

回答

2

你应该是这样的:

$sql = "UPDATE `users` 
      SET `voornaam` = '$voornaam', 
      `achternaam` = '$achternaam', 
        `mail` = '$mailadres', 
        `tel` = '$tel', 
        `adres` = '$adres', 
       `geslacht` = '$geslacht', 
       `bestuur` = '$bestuur', 
        `tc` = '$tc', 
        `ic` = '$ic', 
        `jec` = '$jec', 
        `rvr` = '$rvr', 
      `instructeur` = '$instructeur', 
       `webmaster` = '$webmaster' 
      WHERE id = '".$id."'"; 

这样,如果有空白字段的任何地方你的保护。你也应该确保你清理所有你想插入数据库的条目。

2

您必须引用像“XXXX”否则空字符串任何字符串失踪,串空间看到的2串achternaam =面包车穆勒是错误的,但achternaam ='面包车穆勒是确定也achternaam =“”

2

您需要添加引号'周围的更新值,像

$sql = "UPDATE users SET voornaam = '$voornaam', achternaam = '$achternaam', mail = '$mailadres', tel = '$tel', adres = '$adres', geslacht = '$geslacht', bestuur = '$bestuur', tc = '$tc', ic = '$ic', jec = '$jec', rvr = '$rvr', instructeur = '$instructeur', webmaster = '$webmaster' WHERE id = '".$id."'"; 
2

如果你不知道你的变量查询之前设置,然后检查他们,你创建的SQL查询之前isset

if (!isset($a)) { 
    $a = '' 
} 

,并把你的报价变量:

$sql = "UPDATE users SET voornaam='$voornaam', achternaam='$achternaam', 
     mail='$mailadres', tel='$tel', adres='$adres', geslacht='$geslacht', 
     bestuur='$bestuur', tc='$tc', ic='$ic', jec='$jec', rvr='$rvr', 
     instructeur='$instructeur', webmaster='$webmaster' WHERE id='".$id."'";