1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| <?php
// Encodage (UTF-8)
header('Content-Type: text/html; charset=UTF-8');
// Démarrage de la session
session_start();
// Connexion à la Bdd
try
{
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$pdo_options[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES utf8';
$bdd = new PDO('mysql:host=localhost;dbname=projet', 'root', '', $pdo_options);
}
catch (PDOException $e)
{
die('Erreur : ' . $e->getMessage());
}
// Ajout d'un produit dans le panier
if (isset($_POST['valider'], $_POST['id_produit'], $_POST['quantite']))
{
echo 'hiiii';
$_SESSION['quantite'] = (int)$_POST['quantite'];
$quantite = (int)$_SESSION['quantite'];
$id_client = (int)$_SESSION['id_client'];
$id_produit = (int)$_POST['id_produit'];
$req = $bdd->prepare('INSERT INTO panier (id_produit, quantite, id_client) VALUES (:id_produit, :quantite, :id_client) ');
$req->bindValue(':id_produit', $id_produit, PDO::PARAM_INT);
$req->bindValue(':quantite', $quantite, PDO::PARAM_INT);
$req->bindValue(':id_client', $id_client, PDO::PARAM_INT);
$req->execute();
// Redirection / actualisation
header('Location: achat.php?insert=true');
}
// Récupération des données
$affiche = $bdd->query('SELECT p.quantite, f.titre, f.prix
FROM panier p
INNER JOIN cd_film f ON p.id_produit = f.id_film');
// fetchAll : permet de récupérer tous les éléments d'un seul coup
$panier = $affiche->fetchALL(PDO::FETCH_ASSOC);
// Partie Interface / HTML
?>
<html>
<head>
... etc ...
</head>
<body>
<?php
// SI insertion
if (isset($_GET['insertion']) && $_GET['insertion'] == 'true') {
echo '<p>Le produit a bien été ajouté</p>';
}
?>
<?php
if (!empty($panier)) {
?>
<table>
<tr>
<th>NOM</th>
<th>QUANTITE</th>
<th>PRIX</th>
</tr>
<?php
// affichage de chaque produit
foreach($panier as $affichage_panier) {
?>
<tr>
<td><?php echo $affichage_panier['titre']; ?></td>
<td><?php echo $affichage_panier['quantite']; ?></td>
<td><?php echo $affichage_panier['prix']; ?></td>
</tr>
<?php
}
?>
</table>
<?php
}
else {
echo '<p>Votre panier est vide.</p>';
}
?>
</body> |
Partager