[résolu] Problème Backslash-Requête
Posté : 09 janv. 2012, 15:34
Bonjour,
j'ai un problème qui va finir par me rendre dingue.
L'utilisateur peut à un moment rentrer un commentaire. Mais à partir du moment où il utilise une apostrophe, il y a systématiquement un backslash qui se génère juste avant l'apostrophe.
ex :
Commentaire : je n'ai pas besoin de faire de commentaire
Valider
Commentaire final : je n\'ai pas besoin de faire de commentaire
Alors j'ai trouvé sur un forum une petite fonction :
Requête invalide : 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 'est un test', derniereMAJ = NOW(), datecontact = '1010-10-10', ' at line 9 Requête complète :
UPDATE affaire SET id_societe = '44', id_interlocuteur = '914', id_offre_commerciale = '1', id_repertoire = '75', id_naf = 1, id_division_naf = null, id_region_etude = null, commentaires = 'c'est un test', derniereMAJ = NOW(), datecontact = '1010-10-10', dateligne = '1010-10-10', dateeval = '1010-10-10', typecontact = 'Entretien téléphonique', libtarif = '', pte = '10', hph = '82', hpd = '0', hch = '82', hcd = '0', hpe = '83', hce = '87', ja = '0', label_tarif = 'Basse tension OH & NO Base & EJP (UM, UL)', taille = '0', fourchette = '0', representation = 'histogrammes', choix_taille = 'superficie', annee_etude = '2010' WHERE id_affaire = 'aa'
Est-ce que vous savez comment parer ce problème ?
j'ai un problème qui va finir par me rendre dingue.
L'utilisateur peut à un moment rentrer un commentaire. Mais à partir du moment où il utilise une apostrophe, il y a systématiquement un backslash qui se génère juste avant l'apostrophe.
ex :
Commentaire : je n'ai pas besoin de faire de commentaire
Valider
Commentaire final : je n\'ai pas besoin de faire de commentaire
Alors j'ai trouvé sur un forum une petite fonction :
<?php
function no_magic_quotes($query) {
$data = explode("\\",$query);
$cleaned = implode("",$data);
return $cleaned;
}
// I'm using mysql_escape_string as a simple example, but this function would work for any escaped string.
$query = "It's amaizing! Who's to say this isn't a simple function?";
$badstring = mysql_escape_string($query);
echo '<b>Without funtion:</b> '.$badstring;
echo '<br><br>';
echo '<b>With function:</b> '.no_magic_quotes($badstring);
?>
Output:
Without funtion: It\'s amaizing! Who\'s to say this isn\'t a simple function?
With function: It's amaizing! Who's to say this isn't a simple function?
Je pensais alors m'être débarassé de ce problème, mais alors maintenant, le fait d'avoir enlevé le backslash me crée un problème dans ma requête SQL:Requête invalide : 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 'est un test', derniereMAJ = NOW(), datecontact = '1010-10-10', ' at line 9 Requête complète :
UPDATE affaire SET id_societe = '44', id_interlocuteur = '914', id_offre_commerciale = '1', id_repertoire = '75', id_naf = 1, id_division_naf = null, id_region_etude = null, commentaires = 'c'est un test', derniereMAJ = NOW(), datecontact = '1010-10-10', dateligne = '1010-10-10', dateeval = '1010-10-10', typecontact = 'Entretien téléphonique', libtarif = '', pte = '10', hph = '82', hpd = '0', hch = '82', hcd = '0', hpe = '83', hce = '87', ja = '0', label_tarif = 'Basse tension OH & NO Base & EJP (UM, UL)', taille = '0', fourchette = '0', representation = 'histogrammes', choix_taille = 'superficie', annee_etude = '2010' WHERE id_affaire = 'aa'
Est-ce que vous savez comment parer ce problème ?