Problème ajout en php

Fermé
emy - 23 juin 2010 à 17:48
 emy - 24 juin 2010 à 15:48
salut, je suis entrain de concevoir une application en php/mysql , mon prob c'est que quand je veux insérer un nouvel enregistrement dans la base, un message s'affiche comme quoi "L'insertion a ete correctement effectuée", quand je reviens à ma base de donnée, je trouve une nouvelle ligne insérée mais vide. voila mon script:

<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("new_base",$con);

$ISBN=isset($_POST['IS']);
$CODEED=isset($_GET['CE']);
$NOMLIV=isset($_POST['NL']);
$AUTEUR=isset($_POST['AUT']);
$NUMSERIE=isset($_POST['NS']);
$NB_EXEMPLAIRE=isset($_POST['NE']);
$MOCLE=isset($_POST['motcle']);


$sql = "INSERT INTO livre VALUES ('$ISBN','$CODEED','$NOMLIV','$AUTEUR','$NUMSERIE','$NB_EXEMPLAIRE','$MOCLE')";
$requete = mysql_query($sql, $con) or die( mysql_error() ) ;
if($sql)
{
echo("L'insertion a ete correctement effectuee") ;
}
else
{
echo("L'insertion à echouee") ;
}

?>

25 réponses

<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("new_base",$con);
if(isset($_POST['IS'])){
$ISB = $_POST['IS'];
}
if(isset($_POST['CE'])){
$CODEE = $_POST['CE'];
}
if(isset($_POST['NL'])){
$NOMLI = $_POST['NL'];
}

if(isset($_POST['AUT'])){
$AUTEU = $_POST['AUT'];
}
if(isset($_POST['NS'])){
$NUMSERI = $_POST['NS'];
}

if(isset($_POST['NE'])){
$NB_EXEMPLAIR = $_POST['NE'];
}
if(isset($_POST['motcle'])){
$MOCL = $_POST['motcle'];
}

$sql = "INSERT INTO livre VALUES ('$ISB','$CODEE','$NOMLI','$AUTEU','$NUMSERI','$NB_EXEMPLAIR','$MOCL')";
$requete = mysql_query($sql, $con) or die( mysql_error() ) ;
if($requete)
{
echo("L'insertion a ete correctement effectuee") ;
}
else
{
echo("L'insertion à echouee") ;
}
include"disconnect.php";
?>



sa donne ça
Notice: Undefined variable: ISB in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 32

Notice: Undefined variable: CODEE in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 32

Notice: Undefined variable: NOMLI in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 32

Notice: Undefined variable: AUTEU in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 32

Notice: Undefined variable: NUMSERI in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 32

Notice: Undefined variable: NB_EXEMPLAIR in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 32

Notice: Undefined variable: MOCL in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 32
Duplicata du champ '' pour la clef 'PRIMARY'
0
Leviathan49 Messages postés 257 Date d'inscription jeudi 10 juin 2010 Statut Membre Dernière intervention 22 juillet 2011 70
24 juin 2010 à 14:38
je propose de remplacer le
echo "<td><a href=livreModif.php?ref=$row[0] color=#000000> modifier</a>

par
echo "<td><a href=livreModif.php?IS=$_POST['IS']&CE=$_POST['CE']&NL=$_POST['NL']&AUT=$_POST['AUT']&NE=$_POST['NE']&motcle=$_POST['motcle']&ref=$row[0] color=#000000>"

Et de recuperer les variables dans $_GET donc
<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("new_base",$con);
$ISB = $_GET['IS'];
$CODEE = $_GET['CE'];
$NOMLI = $_GET['NL'];
$AUTEU = $_GET['AUT'];
$NUMSERI = $_GET['NS'];
$NB_EXEMPLAIR = $_GET['NE'];
$MOCL = $_GET['motcle'];
}

$sql = "INSERT INTO livre VALUES ('$ISB','$CODEE','$NOMLI','$AUTEU','$NUMSERI','$NB_EXEMPLAIR','$MOCL')";
$requete = mysql_query($sql, $con) or die( mysql_error() ) ;
if($requete)
{
echo("L'insertion a ete correctement effectuee") ;
}
else
{
echo("L'insertion à echouee") ;
}
include"disconnect.php";
?>

Ce code n'est pas très résistant à l'erreur mais ca devrais peut-être marché si tout les champs sont remplis. Donc il y aura des isset à rajouter dans ListeLivre.php pour gérer ces cas-là.
0
j'ai changer le code du formulaire listelivre

<?php
include "connect.php";

echo "<h3>Liste des Livres</h3>";
$sql = "SELECT * FROM Livre";
$resultat = mysql_query($sql);
echo "[<a href=livreAjoutExec.php>Ajouter un Livre</a>]";
echo "<table border=1 width=75%>";
echo "<tr><td bgcolor=11FF11>IS</td><td bgcolor=11FF11>CE</td><td bgcolor=11FF11>NL</td><td bgcolor=11FF11>AUT</td><td bgcolor=11FF11>NS</td><td bgcolor=11FF11>NE</td><td bgcolor=11FF11>motcle</td><td bgcolor=11FF11>Modification</td><td bgcolor=11FF11>Suppression</td></tr>";
while ($row = mysql_fetch_array($resultat)) {
$ISB=$row[0];$CODEE=$row[1];$NOMLI=$row[2];$AUTEU=$row[3];$NUMSERI=$row[4];$NB_EXEMPLAIR=$row[5];$MOCL=$row[6];
echo "<tr><td>";

echo "<td>";
echo "<input type=text disabled=disable style=width:110px; height:18px; font-family:tahoma; font-size:11px value=".$CODEE.">";
echo "</td>";
echo "<td>";
echo "<input type=text disabled=disable style=width:100px; height:18px; font-family:tahoma; font-size:11px value=".$NOMLI.">";
echo "</td>";
echo "<td>";
echo "<input type=text disabled=disable style=width:110px; height:18px; font-family:tahoma; font-size:11px value=".$AUTEU.">";
echo "</td>";
echo "<td>";
echo "<input type=text disabled=disable style=width:110px; height:18px; font-family:tahoma; font-size:11px value=".$NUMSERI.">";
echo "</td>";
echo "<td>";
echo "<input type=text disabled=disable style=width:110px; height:18px; font-family:tahoma; font-size:11px value=".$NB_EXEMPLAIR.">";
echo "</td>";
echo "<td>";
echo "<input type=text disabled=disable style=width:110px; height:18px; font-family:tahoma; font-size:11px value=".$MOCL.">";
echo "</td>";

echo "<td><a href=livreModif.php?IS=$row[0] color=#000000> modifier</a></td><td><a href=livreSupp.php?IS=$row[0] color=#000000>supprimer</a></td>";
echo "</tr>";
echo "</a>";
echo "</div>";
echo "</td></tr>";
}
echo "</table>";

include "disconnect.php";
?>

et traitemnt ajout comme suit:

<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("new_base",$con);
if(isset($_POST['IS'])){
$ISB = $_POST['IS'];
}
if(isset($_POST['CE'])){
$CODEE = $_POST['CE'];
}
if(isset($_POST['NL'])){
$NOMLI = $_POST['NL'];
}

if(isset($_POST['AUT'])){
$AUTEU = $_POST['AUT'];
}
if(isset($_POST['NS'])){
$NUMSERI = $_POST['NS'];
}

if(isset($_POST['NE'])){
$NB_EXEMPLAIR = $_POST['NE'];
}
if(isset($_POST['motcle'])){
$MOCL = $_POST['motcle'];
}


$sql = "INSERT INTO livre VALUES ('$ISB','$CODEE','$NOMLI','$AUTEU','$NUMSERI','$NB_EXEMPLAIR','$MOCL')";
$requete = mysql_query($sql, $con) or die( mysql_error() ) ;
header('Location: LivreListe.php') ;
if($requete)
{
echo("L'insertion a ete correctement effectuee") ;
}
else
{
echo("L'insertion à echouee") ;
}
include"disconnect.php";
?>


sa donne ça


Notice: Undefined index: IS in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 29

Notice: Undefined index: CE in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 30

Notice: Undefined index: NL in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 31

Notice: Undefined index: AUT in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 32

Notice: Undefined index: NS in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 33

Notice: Undefined index: NE in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 34

Notice: Undefined index: motcle in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 35
Duplicata du champ '' pour la clef 'PRIMARY'
0
CE CODE D4AJOUT ME DONNE DES ERREURS
<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("new_base",$con);
if(isset($_POST['IS'])){
$I = $_POST['IS'];
}
if(isset($_POST['CODEED'])){
$COD = $_POST['CODEED'];
}
if(isset($_POST['NL'])){
$NOM = $_POST['NL'];
}

if(isset($_POST['AUT'])){
$AUT = $_POST['AUT'];
}
if(isset($_POST['NS'])){
$NUMSE = $_POST['NS'];
}

if(isset($_POST['NE'])){
$NB_EXEMPLA = $_POST['NE'];
}
if(isset($_POST['mocle'])){
$MO = $_POST['motcle'];
}
$sql = "INSERT INTO livre VALUES ('$I','$COD','$NOM','$AUT','$NUMSE','$NB_EXEMPLA','$MO')";
$requete = mysql_query($sql, $con) or die( mysql_error() ) ;
header('Location: LivreListe.php') ;
if($requete)
{
echo("L'insertion a ete correctement effectuee") ;
}
else
{
echo("L'insertion à echouee") ;
}
include"disconnect.php";
?>

les erreurs:
Notice: Undefined variable: I in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 38

Notice: Undefined variable: COD in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 38

Notice: Undefined variable: NOM in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 38

Notice: Undefined variable: AUT in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 38

Notice: Undefined variable: NUMSE in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 38

Notice: Undefined variable: NB_EXEMPLA in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 38

Notice: Undefined variable: MO in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 38
Duplicata du champ '' pour la clef 'PRIMARY'
0
maka54 Messages postés 698 Date d'inscription mercredi 8 avril 2009 Statut Membre Dernière intervention 4 décembre 2016 80
24 juin 2010 à 15:47
l'erreur se trouve toujours sur le formulaire ...., tu n'envoie toujours aucune valeur
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
VOICI MON CODE DE LISTELIVRE
<?php
include "connect.php";

echo "<h3>Liste des Livres</h3>";
$sql = "SELECT * FROM Livre";
$resultat = mysql_query($sql);
echo "[<a href=livreAjoutExec.php>Ajouter un Livre</a>]";
echo "<table border=1 width=75%>";
echo "<tr><td bgcolor=11FF11>IS</td><td bgcolor=11FF11>CE</td><td bgcolor=11FF11>NL</td><td bgcolor=11FF11>AUT</td><td bgcolor=11FF11>NS</td><td bgcolor=11FF11>NE</td><td bgcolor=11FF11>motcle</td><td bgcolor=11FF11>Modification</td><td bgcolor=11FF11>Suppression</td></tr>";
while ($row = mysql_fetch_array($resultat)) {

echo "<tr>";
echo "<td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td><td>".$row[3]."</td<td>".$row[4]."</td><td>".$row[5]."</td><td>".$row[6]."</td>";


echo "<td><a href=exemplaireModif.php?ISBN=$row[0] color=#000000> modifier</a></td><td><a href=exemplaireSupp.php?ISBN=$row[0] color=#000000>supprimer</a></td>";
echo "</tr>";
}
echo "</table>";

include "disconnect.php";
?>


<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("new_base",$con);
if(isset($_POST['IS'])){
$I = $_POST['IS'];// IS représente le name du champ de text ds l formulaire jout livre
}
if(isset($_POST['CODEED'])){
$COD = $_POST['CODEED'];
}
if(isset($_POST['NL'])){
$NOM = $_POST['NL'];
}

if(isset($_POST['AUT'])){
$AUT = $_POST['AUT'];
}
if(isset($_POST['NS'])){
$NUMSE = $_POST['NS'];
}

if(isset($_POST['NE'])){
$NB_EXEMPLA = $_POST['NE'];
}
if(isset($_POST['mocle'])){
$MO = $_POST['motcle'];
}
$sql = "INSERT INTO livre VALUES ('$I','$COD','$NOM','$AUT','$NUMSE','$NB_EXEMPLA','$MO')";
$requete = mysql_query($sql, $con) or die( mysql_error() ) ;
header('Location: LivreListe.php') ;
if($requete)
{
echo("L'insertion a ete correctement effectuee") ;
}
else
{
echo("L'insertion à echouee") ;
}
include"disconnect.php";
?>


ce code me donne les erreurs uivantes
Notice: Undefined variable: I in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 38

Notice: Undefined variable: COD in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 38

Notice: Undefined variable: NOM in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 38

Notice: Undefined variable: AUT in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 38

Notice: Undefined variable: NUMSE in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 38

Notice: Undefined variable: NB_EXEMPLA in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 38

Notice: Undefined variable: MO in C:\Program Files\EasyPHP-5.3.2\www\livreAjoutExec.php on line 38
Duplicata du champ '' pour la clef 'PRIMARY'

pouvez vous m'aidez à retrouver les erreurs !!
0