Vous êtes sur la page 1sur 13

PHP et MySql:

Requêtes
Insertion
Suppression
Mise a Jour
Script connexion à la BD

connexion.php
<?php
try
{$connect=new
pdo('mysql:host=localhost;dbname=maBase','root','');
}
catch(Exeption $e)
{die('Erreur:'.$e->getMessage());
}
?>
Requête dans la BD MySQL
affichage_client.php
<?php require_once('connexion.php');
$requete= “SELECT * FROM client”;
$req = $connect->query($requete);
?>
<!DOCTYPE html >
<head> </head>
<body>
<?php
while($donne=$req->fetch())
{ echo $donne['numero']; ?> <br/>
<?php echo $donne['nom']; ?> <br/>
<?php echo $donne['adresse']; ?> <br/>
<?php echo $donne['numero_telephone']; ?> <br/>
<?php } $req->closeCursor();?>
</body>
</html>
Script insertion dans la BD 1/3
formulaire_clients.php
<?php
Require_once("connexion.php");
if (isset($_POST['bouton']) and $_POST['bouton']=='Envoyer')
{ $req = $connect->prepare('INSERT INTO client

VALUES(:numero,:nom,:adresse,:numero_tel)');
$requete=
$req->execute(array('numero'=>'','nom'=>$_POST['nom']
,'adresse'=>$_POST['adresse'],'numero_tel'=>$_POST['numero_tel']
));
$message= '<h3><font color=\"red\">inscription validée</font></h3>';

}
Script insertion dans la BD 2/3

formulaire_clients.php
<?php
require("connexion.php");
if (isset($_POST['bouton']) and $_POST['bouton']=='Envoyer')
{ $req = $connect->prepare('INSERT INTO client VALUES(?,?,?,?)');
$req->execute(array(‘’,$_POST['nom'],$_POST['adresse'],
$_POST['numero_tel']));
$message= '<h3><font color=\"red\">inscription validée</font></h3>';

}
Insertion dans la BD MySQL 3/3
formulaire_clients.php
<!DOCTYPE html >
<head> </head>
<body>
<form id="monform" name="form1" method="post" action="#">
<label>Nom : <input type="text" name="nom" />
</label>
<label>Adresse : <input type="text" name="adresse" />
</label>
<label>Telephone :
<input type="number" name="numero_tel" />
</label>
<label>
<input type="submit" name="bouton" value="Envoyer" />
</label>
</body>
</html>
Notion de clef étrangère
Notion de clef étrangère
• Les champ numéro_client est une clef
étrangère dans la table vente . Il référence le
champ numéro de la table Client
• Les champ réference_produit est une clef
étrangère dans la table vente . Il référence le
champ référence de la table Produit.
Création de clef étrangère
• Création de la clé étrangère réference_produit de
la table vente .
• ALTER TABLE vente ADD FOREIGN KEY
(référence_produit) REFERENCES
produit(référence) ON DELETE RESTRICT ON
UPDATE RESTRICT;
• Création de la clé étrangère numéro_client est
une clef étrangère de la table vente .
• ALTER TABLE vente ADD FOREIGN KEY
(numero_client) REFERENCES client (numero)
ON DELETE RESTRICT ON UPDATE RESTRICT;
Notion de clef étrangère
formulaire_clients.php
<!DOCTYPE html >
<head> </head>
<body>
<form id="monform" name="form1" method="post" action="#">
<label>Nom : <input type="text" name="nom" />
</label>
<label>Adresse : <input type="text" name="adresse" />
</label>
<label>Telephone :
<input type="number" name="numero_tel" />
</label>
<label>
<input type="submit" name="bouton" value="Envoyer" />
</label>
</body>
</html>
Suppression et MAJ de données 1/3
Suppression et MAJ de données 2/3
affichage_clients.php
<!DOCTYPE html >
<head> </head>
<body>
<center>
<table width="600" border="5" solid >
<tr>
<font color="red">
<th><h3>NumeroC</h3></th>
<th><h3>Nom</h3></th>
<th><h3>Adresse</h3></th>
<th><h3>Telephone</h3></th>
<th><h3>Supprimer</h3></th>
<th><h3>Modifier</h3></th>
</font>
</tr>
Suppression et MAJ de données 3/3
<?php while($donne=$req->fetch()); ?>
<tr>
<td><?php echo $donne['numero']; ?></td>
<td><?php echo $donne['nom']; ?></td>
<td><?php echo $donne['adresse']; ?></td>
<td><?php echo $donne['numero_tel']; ?></td>
<td><a href="afficahge_clients.php?id=<?php
echo $donne['numero']; ?>">Supprimer</a></td>
<td><a href="afficahge_clients.php?id=<?php
echo $donne['numero']; ?>">Modifier</a></td>
</tr>
<?php } $req->closeCursor();?>
</table>
</center>
</body>
</html>

Vous aimerez peut-être aussi