-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 'emri='Albana', klienti='Distribrands', telefoni='0662080090', montim='Kompjut' at line 1

我在这里找不到错误..这很奇怪...请帮帮我。这是一个更新查询!查询:

$sqlStart="UPDATE forma SET ";
$sql="";
if ($postDyqani_pergjegjes!= 'undefined') $sql .= " dyqani_pergjegjes='$postDyqani_pergjegjes',";
if ($postEmri!= 'undefined') $sql .= "  emri='$postEmri',";
if ($postKlienti!= 'undefined') $sql .= "  klienti='$postKlienti',";
if ($postTelefoni!= 'undefined') $sql .= "  telefoni='$postTelefoni',";
if ($postMontim!= 'undefined') $sql .= "  montim='$postMontim',";
if ($postAdresa!= 'undefined') $sql .= "  adresa='$postAdresa',";
if ($postData_e_shitjes!= 'undefined') $sql .= "  data_e_shitjes='$postData_e_shitjes',";
if ($postDifekti!= 'undefined') $sql .= "  difekti='$postDifekti',";
if ($postTekniku_emer!= 'undefined') $sql .= "  tekniku_emer='$postTekniku_emer',";
if ($postTekniku_mesazh!= 'undefined') $sql .= "  tekniku_mesazh='$postTekniku_mesazh',";
if ($postData_fillim!= 'undefined') $sql .= "  data_fillim='$postData_fillim',";
if ($postData_mbarim!= 'undefined') $sql .= "  data_mbarim='$postData_mbarim',";
if ($postData!= 'undefined') $sql .= "  data='$postData',";
if ($postStatus!= 'undefined') $sql .= "  status='$postStatus',";
// replace the last `,` for `;`
if ($sql != "") {
    // replace the last `,` for `;`
    $sql = substr($sql, 0, -1) . ";";
    // run sql command
    $sqlCommand = $sqlStart.$sql;
    $result=mysql_query($sql) or die(mysql_error()) ;

} else {
    // no fields to update
}

谢谢

4

2 回答 2

3

您的问题是您正在执行$sql这只是查询的一半。完整的查询是$sqlCommand这样执行的:

 $result=mysql_query($sqlCommand) or die(mysql_error()) ;

旁注:您应该确保对单引号的变量进行转义(请参阅 参考资料mysql_real_escape_string())。该mysql_*库已弃用,因此建议迁移到 PDO 或 MySQLi。

于 2012-12-12T09:40:24.353 回答
0

请用 $sqlCommand 代替 $sql,因为最终查询在此变量中。

于 2012-12-12T09:43:25.033 回答