Php insert et update

Fermé
Mahandritiana Messages postés 14 Date d'inscription vendredi 28 novembre 2014 Statut Membre Dernière intervention 28 janvier 2015 - 26 janv. 2015 à 09:33
Mahandritiana Messages postés 14 Date d'inscription vendredi 28 novembre 2014 Statut Membre Dernière intervention 28 janvier 2015 - 28 janv. 2015 à 06:50
Bonjour,


je ne comprend pas pourquoi dans mon code isert et update PDO, quand le formulaire s'affiche les informations dans la table s'affiche en baas aussi avec alors que ça devrait être suelement le PHP qui doit s'affiche, mais le delete marche bien par contre.
A voir également:

3 réponses

jordane45 Messages postés 38138 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 17 avril 2024 4 649
26 janv. 2015 à 09:56
Bonjour,
Pourrais tu essayer de nous réexpliquer clairement ... et en nous montrant ton code..

PS:Merci de bien vouloir utiliser la coloration syntaxique (les balises de code) lorsque tu postes du code sur le forum.
Explications disponibles ici :
https://codes-sources.commentcamarche.net/faq/10686-le-nouveau-codes-sources-comment-ca-marche#balises-code
0
Mahandritiana Messages postés 14 Date d'inscription vendredi 28 novembre 2014 Statut Membre Dernière intervention 28 janvier 2015
26 janv. 2015 à 17:21
ben voila, quand s'appyue sur mes boutons modifier ou ajouter les formulaires doivent s'afficher mais dans mon cas le formulaire s'affiche mais les informations déjà inserée dans ma table s'affiche en ligne en bas du formulaire aussi alors que ça doit être le formulaire seule!!

voila donc mon code
affichage de la table
fournisseur.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<title>MFB</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

		<!-- Cascading Style Sheets -->
		
    <link rel="stylesheet" type="text/css" media="all" href="4.css" />
		<script type="text/javascript" src="date_heure.js"></script>
</head>
<body>

  <div id="tete">	

<img src="images/cal.gif.jpg" width="100%" height="100%"/>

<h3>
<span id="date_heure"></span>
<script type="text/javascript">window.onload = date_heure('date_heure');</script>
</h3>
 </div>
 <table class="table2">
 <td>
 <div id="main">         
		 <div id="menu">
             <ul>
                <li><a href="Accueil.html">ACCUEIL</a></li>
                <li><a href="site_oui.php">SITE</a></li>
				<li><a href="fournisseur.php">FOURNISSEUR</a></li>
				<li><a href="marche.php">MARCHE</a></li>
				<li><a href="edition.php">EDITION</a></li>
                <li><a href="mada.php">MADA_MAP</a></li>
              </ul>
             
            </div>
   </div>
   </td>
  <td>
<?php
$hostdb = 'localhost';
$userdb = 'root';
$passdb = '';
$namedb = 'mfb';

try {
  $conn = new PDO("mysql:host=".$hostdb."; dbname=".$namedb."", $userdb, $passdb, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
  $conn->exec("SET CHARACTER SET utf8");  

  $sql = "SELECT fournisseur_id, NIF, Nom, Adresse, Tel FROM fournisseur Order by fournisseur_id";
  $result = $conn->query($sql);

  if(!$result) {
            echo 'Impossible d\'afficher la liste de tous les fournisseurs';
		                }
		
		else
		{
			echo '<table class="table1">
			    <caption><center>Liste des Fournisseurs</center></caption>
					<tr>
 					<th scope="col">fournisseur_id</th>
					<th scope="col">NIF</th>
					<th scope="col">Nom</th>
					<th scope="col">Adresse</th>
					<th scope="col">Tel</th>
					
					</tr>';
			
			while( $donnees=$result->fetch(PDO::FETCH_ASSOC))
			{
				echo '
  					<tr>
    					<td>'.$donnees['fournisseur_id'].'</td>
   						<td>'.$donnees['NIF'].'</td>
						<td>'.$donnees['Nom'].'</td>
   						<td>'.$donnees['Adresse'].'</td>
						<td>'.$donnees['Tel'].'</td>
					
  					</tr>
					';
			}
			echo '</table>';
		};

 

  $conn = null;
}
catch(PDOException $e) {
  echo $e->getMessage();
}
?>
<div class="bouton" >

			<a href="ajoutfourni.php"><input type="submit" value="AJOUTER" name="ajout" OnClick="return ajouter" style="background-color: black;
width: 100px;
height:40px;
border-radius: 6px;
color: white;
font-family: algerian;
text-align: center;"></a>
			<a href="modififournisseur.php"><input type="submit" value="MODIFIER" name="modif" onclick="return formulairemodifier()"style="background-color: black;
width: 100px;
height:40px;
border-radius: 6px;
color: white;
font-family: algerian;
text-align: center;"/></a>
			<a href="supprimefournisseur.php"><input type="submit" value="SUPPRIMER" name="suppr"  id="suppr" onclick="return supprimer()"style="background-color: black;
width: 100px;
height:40px;
border-radius: 6px;
color: white;
font-family: algerian;
text-align: center;"/></a>
			
	</div>
</td>
</table>
<div id="footer"> 
<a href="Out.php">Se déconnecter</a>
</div>

</body>
</html>


puis mon code ajouter (insert)
ajoutfourni.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<title>MFB</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

		<!-- Cascading Style Sheets -->
		
    <link rel="stylesheet" type="text/css" media="all" href="3.css" />
		<script type="text/javascript" src="date_heure.js"></script>
</head>
<body>

  <div id="tete">	

<img src="images/cal.gif.jpg" width="100%" height="100%"/>

<h3>
<span id="date_heure"></span>
<script type="text/javascript">window.onload = date_heure('date_heure');</script>
</h3>
 </div>
 <table class="table2">
 <td>
 <div id="main">         
		 <div id="menu">
             <ul>
                <li><a href="Accueil.html">ACCUEIL</a></li>
                <li><a href="site_oui.php">SITE</a></li>
				<li><a href="fournisseur.php">FOURNISSEUR</a></li>
				<li><a href="marche.php">MARCHE</a></li>
				<li><a href="edition.php">EDITION</a></li>
                <li><a href="mada.php">MADA_MAP</a></li>
              </ul>
             
            </div>
   </div>
   </td>
  <td>
<?php
$hostdb = 'localhost';
$userdb = 'root';
$passdb = '';
$namedb = 'mfb';

try
   {
     $conn = new PDO("mysql:host=".$hostdb."; dbname=".$namedb."", $userdb, $passdb, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
     $conn->exec("SET CHARACTER SET utf8");
	}
catch (PDOException $e)
   {
     echo 'La base de donnée n\'est pas disponible';
    }

$erreur_requete = null;
$tableau_fournisseur = array();

try
{
  if(isset($_POST['bouton']))
  {
          $stmt = $conn->prepare("INSERT INTO fournisseur(fournisseur_id, NIF, Nom, Adresse, Tel) VALUES ( ?, ?, ?, ?, ?)");
          $stmt->execute(array($_POST['ID'],$_POST['NIF'],$_POST['NOM'],$_POST['Adresse'],$_POST['Tel']));
          print_r($stmt->errorInfo());
            
          header('Location: fournisseur.php');
          exit;
  }
        else
        {
                $req = "SELECT * FROM fournisseur";
                $res = $conn->query($req);
               
               
                $tableau_fournisseur = $res->fetchAll(PDO::FETCH_ASSOC);
               
                
                $res->closeCursor();
        }
}
catch(PDOException $e)
{
   
   $erreur_requete = "Problème dans la requête";

     $erreur_requete .= " : ".$e->getMessage();
}

?>

    <div>
          
        <?= isset($erreur_requete) ? $erreur_requete : '';?>
    </div>
   
    <form name="form" action="ajoutfourni.php" method="POST" enctype="multipart/form-data" >
		        <fieldset>
		    <legend><em>Ajout d'un Fournisseur</em></legend>
			<table align="center">
			<tr>
			    <td></td>
			</tr>
			<tr>
			    <td>
				    <label for="ChampID">Fournisseur ID:</label>
				</td>
				<td>
					<input type="text" id="ID" name="ID" require/>
				</td>
			</tr>
		
			<tr>
			    <td>
				    <label for="ChampNIF">Fournisseur NIF:</label>
				</td>
				<td>
					<input type="text" id="NIF" name="NIF" require/>
				</td>
			</tr>
			
			<tr>
				<td>
				    <label for="ChampNom">Nom du Fournisseur:</label>
				</td>
				<td>
				    <input type="text" id="NomRespo" name="NomRespo" require/>
				</td>
		    </tr>
			<tr>
				<td>
				    <label for="ChampAdresse">Adresse du Fournisseur:</label>
				</td>
				<td>
				    <input type="text" id="Adresse" name="Adresse" require/>
				</td>
		    </tr>
			<tr>
				<td>
				    <label for="ChampTel">Téléphone du Fournisseur:</label>
				</td>
				<td>
				    <input type="text" id="Tel" name="Tel" require/>
				</td>
		    </tr>
		   
        <tr>
		<td></td>	
</br>		
	<td>
	    <form action="ajoutfourni.php" method="POST">
	        <input type="submit" name="bouton" value="Ajouter" onclick="alert('Ajout avec succes');" style="background-color: black;
width: 100px;
height:30px;
border-radius: 6px;
color: white;
font-family: algerian;
text-align: center;" /> 
		    <a href='fournisseur.php'><input class='button' type='button' value='Annuler' style="background-color: black;
width: 100px;
height:30px;
border-radius: 6px;
color: white;
font-family: algerian;
text-align: center;"></a>
	    </form>
	</td>
			</tr>
		</table>
		</fieldset>
	</form>
   
        <div>
    <?php foreach($tableau_fournisseur as $value)
        {
               echo htmlspecialchars($value['fournisseur_id']) . " " . htmlspecialchars($value['NIF']) . " " . htmlspecialchars($value['Nom']) . " " . htmlspecialchars($value['Adresse']) . " " . htmlspecialchars($value['Tel']) ."<br>";
        }
        ?>
    </div>
</td>
</table>
<div id="footer"> 
<a href="Out.php">Se déconnecter</a>
</div>

</body>
</html>


et enfin mon code modifier (update)
modififournisseur.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<title>MFB</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

		<!-- Cascading Style Sheets -->
		
    <link rel="stylesheet" type="text/css" media="all" href="3.css" />
		<script type="text/javascript" src="date_heure.js"></script>
</head>
<body>

  <div id="tete">	

<img src="images/cal.gif.jpg" width="100%" height="100%"/>

<h3>
<span id="date_heure"></span>
<script type="text/javascript">window.onload = date_heure('date_heure');</script>
</h3>
 </div>
 <table class="table2">
 <td>
 <div id="main">         
		 <div id="menu">
             <ul>
                <li><a href="Accueil.html">ACCUEIL</a></li>
                <li><a href="site_oui.php">SITE</a></li>
				<li><a href="fournisseur.php">FOURNISSEUR</a></li>
				<li><a href="marche.php">MARCHE</a></li>
				<li><a href="edition.php">EDITION</a></li>
                <li><a href="mada.php">MADA_MAP</a></li>
              </ul>
             
            </div>
   </div>
   </td>
  <td>
<?php
$hostdb = 'localhost';
$userdb = 'root';
$passdb = '';
$namedb = 'mfb';

try
   {
     $conn = new PDO("mysql:host=".$hostdb."; dbname=".$namedb."", $userdb, $passdb, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
     $conn->exec("SET CHARACTER SET utf8");
	}
catch (PDOException $e)
   {
     echo 'La base de donnée n\'est pas disponible';
    }

$erreur_requete = null;
$tableau_fournisseur = array();

try
{
  if(isset($_POST['bouton']))
  {
          $stmt = $conn->prepare("UPDATE SET fournisseur(fournisseur_id, NIF, Nom, Adresse, Tel) VALUES ( ?, ?, ?, ?, ?)");
          $stmt->execute(array($_POST['ID'],$_POST['NIF'],$_POST['NOM'],$_POST['Adresse'],$_POST['Tel']));
          print_r($stmt->errorInfo());
            
          header('Location: fournisseur.php');
          exit;
  }
        else
        {
                $req = "SELECT * FROM fournisseur";
                $res = $conn->query($req);
               
               
                $tableau_fournisseur = $res->fetchAll(PDO::FETCH_ASSOC);
               
                
                $res->closeCursor();
        }
}
catch(PDOException $e)
{
   
   $erreur_requete = "Problème dans la requête";

     $erreur_requete .= " : ".$e->getMessage();
}

?>

    <div>
          
        <?= isset($erreur_requete) ? $erreur_requete : '';?>
    </div>
   
    <form name="form" action="ajoutfourni.php" method="POST" enctype="multipart/form-data" >
		        <fieldset>
		    <legend><em>Modification d'un Fournisseur</em></legend>
			<table align="center">
			<tr>
			    <td></td>
			</tr>
			<tr>
			    <td>
				    <label for="ChampID">Fournisseur ID:</label>
				</td>
				<td>
					<input type="text" id="ID" name="ID" require/>
				</td>
			</tr>
		
			<tr>
			    <td>
				    <label for="ChampNIF">Fournisseur NIF:</label>
				</td>
				<td>
					<input type="text" id="NIF" name="NIF" require/>
				</td>
			</tr>
			
			<tr>
				<td>
				    <label for="ChampNom">Nom du Fournisseur:</label>
				</td>
				<td>
				    <input type="text" id="NomRespo" name="NomRespo" require/>
				</td>
		    </tr>
			<tr>
				<td>
				    <label for="ChampAdresse">Adresse du Fournisseur:</label>
				</td>
				<td>
				    <input type="text" id="Adresse" name="Adresse" require/>
				</td>
		    </tr>
			<tr>
				<td>
				    <label for="ChampTel">Téléphone du Fournisseur:</label>
				</td>
				<td>
				    <input type="text" id="Tel" name="Tel" require/>
				</td>
		    </tr>
		   
        <tr>
		<td></td>	
</br>		
	<td>
	    <form action="ajoutfourni.php" method="POST">
	        <input type="submit" name="bouton" value="Modifier" onclick="alert('Ajout avec succes');" style="background-color: black;
width: 100px;
height:30px;
border-radius: 6px;
color: white;
font-family: algerian;
text-align: center;" /> 
		    <a href='fournisseur.php'><input class='button' type='button' value='Annuler' style="background-color: black;
width: 100px;
height:30px;
border-radius: 6px;
color: white;
font-family: algerian;
text-align: center;"></a>
	    </form>
	</td>
			</tr>
		</table>
		</fieldset>
	</form>
   
        <div>
    <?php foreach($tableau_fournisseur as $value)
        {
               echo htmlspecialchars($value['fournisseur_id']) . " " . htmlspecialchars($value['NIF']) . " " . htmlspecialchars($value['Nom']) . " " . htmlspecialchars($value['Adresse']) . " " . htmlspecialchars($value['Tel']) ."<br>";
        }
        ?>
    </div>
</td>
</table>
<div id="footer"> 
<a href="Out.php">Se déconnecter</a>
</div>

</body>
</html>

merci!!
0
Mahandritiana Messages postés 14 Date d'inscription vendredi 28 novembre 2014 Statut Membre Dernière intervention 28 janvier 2015
28 janv. 2015 à 06:50
il n'y a personne pour me repondre ici!!?
0