2011-03-18 232 views
-2

当我尝试执行更新语句我得到了以下错误:Erreur:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法错误;

Erreur : SQLSTATE[42000]: Syntax error or access violation: 1064 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 'Issy-les-Moulineaux ' where ssiphone_idstation=46' at line 1 

我的更新语句是:

$bdd->exec("update ssiphone_stationdeservice set $cle='$element' where ssiphone_idstation=$id"); 

这是在PHP代码,THX在您的帮助:)

$ CLE和$元素都在阵,我的代码是:

foreach($table1 as $cle => $element) 
    { 
    $bdd->exec("update ssiphone_stationdeservice set $cle='$element' where ssiphone_idstation=$id"); 
    } 

现在表1是包含我的表和它的值的列名的数组:

$table1=array(); 
    $table1['ssiphone_etatstation']=$etat; 
    $table1['ssiphone_commerce']=$commerce; 
    $table1['ssiphone_stationdelavage']=$lavage; 
    $table1['ssiphone_typescarburants']=$lescarburants; 
    $table1['ssiphone_joursdelasemaine']=$jourssemaines; 
    $table1['ssiphone_horaires ']=$this->horaires; 
    $table1['ssiphone_telephone ']=$telephone; 
    $table1['ssiphone_sensdecirculation ']=$this->sensDeCirculation; 
    $table1['ssiphone_adresse ']=$this->adresse; 
    $table1['ssiphone_ville']=$this->ville; 
    $table1['ssiphone_departement']=$this->departement; 
    $table1['ssiphone_nomstation ']=$this->nomStation; 
+0

您是否为'$ cle','$ element'和'$ id'设置了值? – 2011-03-18 14:17:17

+0

没有'$ cle','$ element'和'$ id'的值,很难说。我衷心希望你能够非常全面地验证这些输入--SQL注入就在角落。 – Mat 2011-03-18 14:18:18

+0

列名不能在其名称中包含'-'标志。您的查询设计错误。 – Furicane 2011-03-18 14:20:41

回答

1

最有可能你的$ CLE变量没有设置,使得查询的样子:

... set ='Issy-les-moulineaux ' where ... 

评论随访:

更改您的代码看起来像这样的话:

$query = "update ssiphone_stationdeservice set $cle='$element' where ssiphone_idstation=$id"; 
$result = $bdd->exec($query); 
if ($result === FALSE) { 
    print_r($bdd->errorInfo()); 
    die("Query: " . $query); 
} 

这样你就可以检查一个变量中的完整查询字符串(例如,通过回声)。很明显,查询有问题 - 但是mysql错误字符串不显示整个查询,所以你必须采取措施来捕获它。

+0

我没有理解你的答案,我的值不是手动插入,而是通过一个数组,所以我没有任何介入时执行语句:) – Malloc 2011-03-18 14:41:42

+0

嗨,我认为问题是从一个已存在的地址包含'caracter,我的问题是如何防止这种情况时,在数据库中的表示,thx :) – Malloc 2011-03-18 14:56:39

+0

您正在使用PDO,所以看看使用预准备语句和绑定参数:http://ca3.php.net/manual/en/pdo.prepare.php。那些处理任何SQL元字符如'''。如果您使用的是像mysql/mysqli函数这样的osmething,您可以使用mysql(i)_real_escape_string()函数手动执行相同的操作。 – 2011-03-18 14:58:38

相关问题