2012-12-12 156 views
-1

我有这个疑问,我为了更新表使用,问题是我GE这个错误:错误SQL语法

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

所以我们应该猜测你的更新查询是什么? –

+0

不,我只是找不到错误..让我在这里写下查询.. –

+1

请给我们提供您的代码......至少查询.. –

回答

3

你的问题是你正在执行$sql这仅仅是查询的一半。完整的查询是$sqlCommand所以执行是:

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

旁注:你应该确保这两个变量被转义单引号(见mysql_real_escape_string())。 mysql_*库已弃用,因此建议迁移到PDO或MySQLi。

+1

哦,这真是太愚蠢了! –

+0

我终于终于解决了:') 非常感谢! –

0

请写$ sqlCommand来代替$ sql,因为最终查询在这个变量中。