我的代码的目的是从 CSV 文件中提取数据并将其上传到我的数据库中。
我可以从文件的每一行中提取所有属性,但它一直在 sql 查询中显示错误。
这是我的 PHP 文件:
<?
$row = 1;
$server="XXXXX";
$user="XXXX";
$password="XXXX";
$db="XXXX";
mysql_connect($server,$user,$password) or die('erreur au serveur');
mysql_select_db($db) or die('erreur db');
if (($handle = fopen('XXXX/articles.csv','r+')) !== FALSE) {
while (($data = fgetcsv($handle,";")) !== FALSE) {
$num = count($data);
//echo "<p> $num champs à la ligne $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
$produit =$data[$c];
$att = explode(";", $produit);
$prod = $att[0]; echo 'id = '.$prod .'<br/>';
$code = $att[1]; echo 'code = '.$code.'<br/>';
$nom = $att[2]; echo 'nom = '.$nom.'<br/>';
$cat = $att[3]; echo 'categorie = '.$cat.'<br/>';
$prix = $att[4]; echo 'prix = '.$prix.'<br/>';
$cond = $att[5]; echo 'cond = '.$cond.'<br/>';
$date = $att[6]; echo 'date = '.$date.'<br/>';
$qtes = $att[7]; echo 'qtes = '.$qtes.'<br/>';
$photo = $att[8]; echo 'photo = '.$photo.'<br/>';
$qte = $att[9]; echo 'qte = '.$qte.'<br/>';
$cam = $att[10]; echo 'camion = '.$cam;
$sql = 'UPDATE produit SET code_barre ='.$code.',nom_prod ='.$nom.', photo ='.$photo.',categorie='.$cat.',condition ='.$cond.',prix_uniraire ='.$prix.', date_exp='.$date.' ,qte ='.$qte.',qte_stock ='.$qtes.', id_camion= '.$cam.' WHERE id_prod ='.$prod.'';
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
$row++;
}
}
fclose($handle);
}
?>
这是我得到的:
Notice: 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 'condition =emballés,prix_uniraire =3450, date_exp=04/02/2016 ,qte =200,qte_stock' at line 1 in UPDATE produit SET code_barre =345123,nom_prod =Nutella, photo =www.google.com,categorie=Chocolat,condition =emballés,prix_uniraire =3450, date_exp=04/02/2016 ,qte =200,qte_stock =2100, id_camion= 2 WHERE id_prod =3 in /home/a2258793/public_html/seekarticles.php on line 34
最佳答案
注意问题的开始,MySQL 会告诉你问题从哪里开始:
(旁注:对于您问题下的评论部分,它们同样重要)。
for the right syntax to use near 'condition ^ the problem starts here
"condition" is a MySQL reserved word and requires special attention.
Either you wrap the column name in ticks, or rename it to another word, say "conditions" in plural form. It isn't a MySQL reserved word.
`condition` ='.$cond.'
- 如果上述方法失败,那是因为您的引用方法。引用下面我的建议。
您还可以/应该将该行重写为:(并使用不同的引用方法),因为您的值包含字符串。剩下的,MySQL 会处理整数。
$sql = "UPDATE produit SET code_barre ='".$code."',
nom_prod ='".$nom."', photo ='".$photo."',categorie='".$cat."',
`condition` ='".$cond."',prix_uniraire ='".$prix."',
date_exp='".$date."' ,qte ='".$qte."',
qte_stock ='".$qtes."', id_camion= '".$cam."'
WHERE id_prod ='".$prod."' ";
注意:只有字符串值需要用引号引起来。您可以根据需要进行修改。
即:WHERE id_prod = $prod";
如果 $prod
是包含您的列的整数。
旁注:
您现在的密码是开放给SQL injection .使用 mysqli
with prepared statements , 或 PDO with prepared statements ,它们更安全。
脚注:
如果遇到任何其他错误,您需要使用
mysql_real_escape_string()
转义您的值.MySQL 会提示撇号等。即:
Nutella's the best!
并将其解释为 VALUES 中的'Nutella's the best!'
导致语法错误。而转义数据会将其解释为'Nutella's the best!'
使其有效,因为它已被转义。
关于php - SQL语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30257462/