Erreur injection sql

Eléphant du PHP | 372 Messages

24 juin 2015, 23:17

Salut à tous, bon j'ai finis mon code, l'insertion se fait bien en bdd mais ca bloque au bout d'un moment.
Et j'ai le message suivant en retour d'erreur:

Erreur SQL !INSERT INTO `loisirs` (id_simply_user,titre,price,photo,url_fnac,date_debut_sortie,date_fin_sortie,description,street,postcode,city,id_region,id_departements,id_type_loisirs,valide) VALUES ('999','CHPTS DE FRANCE D\'ATHLETISME ELITE','5.00','http://www.fnacspectacles.com/static/0/ ... 09851&ULP=[[/place-spectacle/manifestation/Athletisme-CHPTS-DE-FRANCE-D-ATHLETISME-ELITE-1ELIT.htm]]','2015-07-10T00:00:00','2015-07-10T00:00:00','blablabla','AV. DE LA CHATELLENIE','FR','VILLENEUVE D'ASCQ','17','59','6','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 'ASCQ','17','59','6','1')' at line 1

Ce serait un problème d'accent ou quelque chose comme ca ?

Le code:
<?php
     
    include("include/variables.php");
     
    //connection a la base de donnee
    $dbhost = "localhost";
    $dbuser = "root";      // mysql user
    $dbpass = "";       // mysql password
    $dbname = "mabase";     // mysql database
    $conn=mysql_connect($dbhost,$dbuser,$dbpass) or die(erreurServeurMySQL());
    mysql_select_db($dbname,$conn) or die('Erreur de selection '.mysql_error()); // problème sur la bdd
      
    $flux = simplexml_load_file('http://localhost/csv/zxpd_201506240433_3467_31592008.xml'); // Source du flux.xml
	
	set_time_limit(20);
	
	// $aLoisirs = array (1 => 'Restaurant', 2 => 'Discothèque', 3 => 'Bar/Pub', 4 => 'Videgrenier/Brocante', 5 => 'Marché', 6 => 'Sport', 7 => 'Loto', 13 => 'Exposition', 10 => 'Divers', 10 => 'Concert', 11 => 'Spectacle', 12 => 'Rencontre', 13 => 'Excursion/Visite', 14 => 'Bal/Festival', 15 => 'Théatre', 16 => 'Classique', 17 => 'Salons et foires',  18 => 'Cinéma', 20 => 'Coffrets cadeau'  );
     
	 
	
	 
    $type  = array(    
                          
            			 // Coffret cadeau
                         'CCA|Carte cadeau' => 20, 'COB|Coffrets Bien-être' => 20, 'COS|Coffrets Séjours' => 20, 'COG|Coffrets Gastronomie' => 20, 'COA|Coffrets Activités' => 20,
                         'COE|Coffrets Enfants' => 20, 'CHM|Coffrets Homme' => 20, 'COS|Coffrets Femme' => 20, ' CPS|Coffrets Sport/Pilotage' => 20, ' CAO|Coffrets Originaux/Atypiques' => 20,
                         'CMT|Coffrets Multithématiques' => 20, 'CLU|Coffrets luxe' => 20, 'CDI|Coffrets divers' => 20,
                          
                         // Cinéma
                         'FIL|Film' => 18, 'ACI|Abonnement/Pass cinéma' => 18, 'ACT|Action' => 18, 'ANI|Animation' => 18, 'ARM|Arts martiaux' => 18, 'AVE|Aventure' => 18, 'AVP|Avant-première' => 18, 'BIO|Biopic' => 18, 'BOL|Bollywood' => 18, 'CIC|Ciné-concert' => 18,
                         'CIE|Cinéma jeune public' => 18, 'CLA|Classique' => 18, 'COD|Comédie dramatique' => 18, 'COM|Comédie' => 18, 'DEA|Dessin animé' => 18, 'DIV|Divers' => 18, 'DOC|Documentaire' => 18, 'DRA|Drame' => 18, 'EPO|Epouvante-horreur' => 18, 'ERO|Erotique' => 18,
                         'ESP|Espionnage' => 18, 'FAN|Fantastique ' => 18, 'FCE|Comédie érotique' => 18, 'FFA|Famille' => 18, 'GUE|Guerre' => 18, 'HIS|Historique' => 18, 'JUD|Judiciaire' => 18, 'MED|Médical' => 18, 'MOB|Mobisode' => 18, 'MUS|Musical' => 18,
                         'NUI|Nuit à thème (cinéma)' => 18, 'PEP|Péplum' => 18,  'POL|Policier' => 18, 'ROM|Romance' => 18, 'SCF|Science fiction' => 18, 'SOA|Soap' => 18, 'THR|Thriller' => 18,
                         'WEB|Web série' => 18, 'WES|Western' => 18, '131F|Cinéma' => 18, 18, '13CI|Cinéma' => 18, 'ROC|Retransmission Opéra/Concert ;' => 18,
                        );
 
     
	
	                       
    foreach ($flux->product as $item) {
		

  
    $titre = addslashes($item->name);
    $price = $item->price;
    $url_fnac = $item->deepLink;
    $date_debut_sortie = $item->validFrom;
    $date_fin_sortie = $item->validTo;
    $street = addslashes($item->terms);
    $photo = $item->largeImage;
    $genre = $item->merchantCategoryPath;
	$description = 'Une sortie proposé par notre parnetaire Fnac, trouvez la sortie du moment qui vous correspond';
	
    // On fait un explode afin de récupérer ce qu'il nous interesse
    $postcode  = $item->extra2;
    $pieces = explode(" | ", $postcode);
 
    // On attribut le département en fonction des 2 premiers chiffre du code postal
    $departement = substr($postcode, 0, 2);
     
    $region = regionIdByDepartement($departement);
     
    // Attribution de la valeur id 1010 pour la fnac
    $id_simply_user = '999';
     
    // On valide la sortie par default
    $valide = '1';
	
	
	foreach (explode(' ; ', /*(string)*/ $item->merchantCategoryPath) as $subtype) {
	$id_type_loisirs = $type[$subtype];
	
	}  
	
	
	/*$result = mysql_query("SELECT id_loisirs FROM loisirs WHERE titre = ".$titre.", city = ".$pieces[2].", postcode = ".$pieces[1].", date_debut_sortie = ".$date_debut_sortie.", date_fin_sortie = ".$date_fin_sortie."");
	if(mysql_num_rows($result) == 1){
		
	//Pseudo déjà utilisé
	echo 'Cette entrée existe déjà';
	
	}else{*/
		
	// Pseudo libre
	$sql = "INSERT INTO `matable` (id_simply_user,titre,price,photo,url_fnac,date_debut_sortie,date_fin_sortie,description,street,postcode,city,id_region,id_departements,id_type_loisirs,valide) VALUES ('$id_simply_user','$titre','$price','$photo','$url_fnac','$date_debut_sortie','$date_fin_sortie','$description','$street','$pieces[2]','$pieces[1]','$region','$departement','$id_type_loisirs','$valide')";
	$req = mysql_query($sql) or die('Erreur SQL !'.$sql.'<br>'.mysql_error());
	echo 'Insertion en bdd';
	}
	//}
?>

Mammouth du PHP | 688 Messages

25 juin 2015, 00:21

non, de ' : 'VILLENEUVE D'ASCQ'

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 10684 Messages

25 juin 2015, 12:19

Tof a déjà répondu, mais pour compléter un peu, MySQL t'indique précisément l'endroit de ta requête où le problème a été rencontré :
check the manual that corresponds to your MySQL server version for the right syntax to use near ' ASCQ','17','59','6','1') '

En regardant la coloration syntaxique de ta requête et en recherchant le ASCQ, tu devrais identifier le problème :
INSERT INTO `loisirs` (id_simply_user,titre,price,photo,url_fnac,date_debut_sortie,date_fin_sortie,description,street,postcode,city,id_region,id_departements,id_type_loisirs,valide) VALUES ('999','CHPTS DE FRANCE D\'ATHLETISME ELITE','5.00','http://www.fnacspectacles.com/static/0/visuel/300/309/CHAMPIONNATS-DE-FRANCE-D-ATHLE_3093336202304347437.jpg?1433931584000','http://ad.zanox.com/ppc/?31592008C778809851&ULP=[[/place-spectacle/manifestation/Athletisme-CHPTS-DE-FRANCE-D-ATHLETISME-ELITE-1ELIT.htm]]','2015-07-10T00:00:00','2015-07-10T00:00:00','blablabla','AV. DE LA CHATELLENIE','FR','VILLENEUVE D'ASCQ','17','59','6','1')
Et je pense que dans ton cas, la suggestion de Moogli de passer sur PDO et d'utiliser les requêtes préparées ou a défaut la protection des données, te permettrait d'optimiser ton code et le sécuriser :)
Ce n'est pas en améliorant la bougie que l'on a inventé l'ampoule...