Page 1 sur 1

Erreurs syntaxe:

Posté : 23 août 2007, 10:27
par invité145
Bonjour,

Je creer une interface administration pour une base de données mais j'ai des soucis de syntaxe; il me dit "SQL syntax near '$sql = "INSERT INTO `medicaments` ( `id` , `nom` , `mise a jour` , `denominatio' at line 1 "

Voici le code:

if(isset($_POST['id'])) { $id = addslashes($_POST['id']); } else { $id = ''; }
if(isset($_POST['nom'])) { $nom = addslashes($_POST['nom']); } else { $nom = ''; }
if(isset($_POST['mise a jour'])) { $mise_a_jour = addslashes($_POST['mise a jour']); } else { $mise_a_jour = ''; }
if(isset($_POST['denomination'])) { $denomination = addslashes($_POST['denomination']); } else { $denomination = ''; }
if(isset($_POST['composition'])) { $composition = addslashes($_POST['composition']); } else { $composition = ''; }


$ajout = ($nom != '' && $mise_a_jour != '' && $denomination != ''&& $composition != '' );

if($ajout) {
	if(isset($_GET['action']) && $_GET['action'] == 'ajouter') {
		$sql = "INSERT INTO `medicaments` ( `id` , `nom` , `mise a jour`  , `denomination` , `composition` )
			VALUES (
			NULL , '".$nom."', '".$mise_a_jour."','".$denomination."', '".$composition."'
)
			";
		if(mysql_query($sql)) {
			echo "médicament ajoutée avec succès. ";
		} else {
			echo "Il y a des erreurs....".mysql_error();
		}
	}
	if(isset($_GET['action']) && $_GET['action'] == 'modifier') {
		$sql = "UPDATE `medicaments`,	
		`nom` = '".$nom."', 
		`mise a jour` = '".$mise_a_jour."',
		`denomination` ='".$denomination."',
		`composition` = '".$composition."' WHERE `id` = '".addslashes($_GET['id'])."' LIMIT 1 ";			
		if(mysql_query($sql)) {
			echo "Médicament modifiée avec succès. ";
		} else {
			echo "Il y a des erreurs....".mysql_error();
		}
	}
} else {
	if(isset($_GET['id'])) {
		/* modification */
		$page = mysql_fetch_array(mysql_query("select * from medicaments where id = '".addslashes($_GET['id'])."' limit 1"));
		echo '<form action="ajouter.php?action=modifier&id='.addslashes($_GET['id']).'" method="post">';						
		echo '<input type="text" name="nom" value="'.stripslashes($page['nom']).'" style="width: 100%"><br />
		<textarea name="mise a jour" style="width: 100%; height: 100px">'.stripslashes($page['mise a jour']).'</textarea><br />
		<textarea name="denomination" style="width: 100%; height: 100px">'.stripslashes($page['denomination']).'</textarea><br />
			<textarea name="composition" style="width: 100%; height: 100px">'.stripslashes($page['composition']).'</textarea><br />				
			<input type="submit" style="width: 100%"><br />			
		</form>';
	} else {
		echo '<form action="ajouter.php?action=ajouter" method="post">';					
			echo '<input type="text" name="nom" value="nom" ondblclick="this.value=\'\'" style="width: 100%"><br />			
			<textarea name="mise a jour" style="width: 100%; height: 100px" ondblclick="this.value=\'\'">mise a jour</textarea><br />
		<textarea name="denomination" style="width: 100%; height: 100px"ondblclick="this.value=\'\'">denomination</textarea><br />		
	<textarea name="composition" style="width: 100%; height: 100px" ondblclick="this.value=\'\'">composition</textarea><br />					
			<input type="submit" style="width: 100%"><br />	
		</form>';
	}
}
Merci

Posté : 23 août 2007, 10:40
par Ryle
Je ne crois pas que les espaces soit tolérés dans les noms des champs : "mise a jour"

Posté : 23 août 2007, 13:18
par Invité
J'ai modifié, mais j'ai toujours la meme erreur:


if(isset($_POST['id'])) { $id = addslashes($_POST['id']); } else { $id = ''; }
if(isset($_POST['nom'])) { $nom = addslashes($_POST['nom']); } else { $nom = ''; }
if(isset($_POST['mise_a_jour'])) { $mise_a_jour = addslashes($_POST['mise_a_jour']); } else { $mise_a_jour = ''; }
if(isset($_POST['denomination'])) { $denomination = addslashes($_POST['denomination']); } else { $denomination = ''; }
if(isset($_POST['composition'])) { $composition = addslashes($_POST['composition']); } else { $composition = ''; }


$ajout = ($nom != '' && $mise_a_jour != '' && $denomination != ''&& $composition != '' );

if($ajout) {
	if(isset($_GET['action']) && $_GET['action'] == 'ajouter') {
		$sql = "INSERT INTO `medicaments` ( `id` ,`nom`,`mise_a_jour`,`denomination`,`composition`)
			VALUES (
			NULL , '".$nom."', '".$mise_a_jour."','".$denomination."', '".$composition."'
)
			";
		if(mysql_query($sql)) {
			echo "médicament ajoutée avec succès. ";
		} else {
			echo "Il y a des erreurs....".mysql_error();
		}
	}
	if(isset($_GET['action']) && $_GET['action'] == 'modifier') {
		$sql = "UPDATE `medicaments`,	
		`nom` = '".$nom."', 
		`mise_a_jour` = '".$mise_a_jour."',
		`denomination` ='".$denomination."',
		`composition` = '".$composition."' WHERE `id` = '".addslashes($_GET['id'])."' LIMIT 1 ";			
		if(mysql_query($sql)) {
			echo "Médicament modifiée avec succès. ";
		} else {
			echo "Il y a des erreurs....".mysql_error();
		}
	}
} else {
	if(isset($_GET['id'])) {
		/* modification */
		$page = mysql_fetch_array(mysql_query("select * from medicaments where id = '".addslashes($_GET['id'])."' limit 1"));
		echo '<form action="ajouter.php?action=modifier&id='.addslashes($_GET['id']).'" method="post">';						
		echo '<input type="text" name="nom" value="'.stripslashes($page['nom']).'" style="width: 100%"><br />
		<textarea name="mise a jour" style="width: 100%; height: 100px">'.stripslashes($page['mise_a_jour']).'</textarea><br />
		<textarea name="denomination" style="width: 100%; height: 100px">'.stripslashes($page['denomination']).'</textarea><br />
			<textarea name="composition" style="width: 100%; height: 100px">'.stripslashes($page['composition']).'</textarea><br />				
			<input type="submit" style="width: 100%"><br />			
		</form>';
	} else {
		echo '<form action="ajouter.php?action=ajouter" method="post">';					
			echo '<input type="text" name="nom" value="nom" ondblclick="this.value=\'\'" style="width: 100%"><br />			
			<textarea name="mise a jour" style="width: 100%; height: 100px" ondblclick="this.value=\'\'">mise a jour</textarea><br />
		<textarea name="denomination" style="width: 100%; height: 100px"ondblclick="this.value=\'\'">denomination</textarea><br />		
	<textarea name="composition" style="width: 100%; height: 100px" ondblclick="this.value=\'\'">composition</textarea><br />					
			<input type="submit" style="width: 100%"><br />	
		</form>';
	}
}
??

Posté : 23 août 2007, 13:27
par Ryle
Je vois pas trop pour l'insertion... affiche la requête générée, assure toi que les noms des champs et de la table sont bons, que les valeurs sont correctes... pour simplifier tu peux également virer les ` qui ne servent à rien ...

Par contre pour le update, tu as une virgule en trop après le nom de la table, il te manque le mot clé SET et tu utilises la commande LIMIT qui est réservée aux selects..

Posté : 23 août 2007, 15:16
par Vikchill
Ce qui me turlupine, c'est que ta requête envoie un null pour le champs id :shock: C'est laid :mrgreen:

Posté : 23 août 2007, 15:56
par dunbar
Ce qui me turlupine, c'est que ta requête envoie un null pour le champs id :shock: C'est laid :mrgreen:
Je me trompe peut-être mais
if(isset($_POST['id'])) { $id = addslashes($_POST['id']); } else { $id = ''; } 
et
WHERE `id` = '".addslashes($_GET['id'])."' LIMIT 1 "
Y pas comme un soucis :?:

Cela ne devrais pas être :!:
if(isset($_GET['id'])) { $id = addslashes($_GET['id']); } else { $id = ''; }