-
Notifications
You must be signed in to change notification settings - Fork 1
/
valideEnd.php
48 lines (38 loc) · 1.28 KB
/
valideEnd.php
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
<?PHP
session_start();
$bdd = new PDO('mysql:host=localhost;dbname=serveur;charset=utf8', 'root', 'password');
$cmd = Array();
$prix_total = 0;
$stmt = $bdd->prepare("UPDATE produits SET nbr = :nbr WHERE id = :id");
foreach ($_SESSION['list'] as $key => $nbr)
{
$res = $bdd->query('SELECT * FROM produits WHERE id='.$key)->fetch();
$nbrStock = $res['nbr'];
$res = $bdd->query('SELECT * FROM prix WHERE id='.$res['id_prix'])->fetch();
// Use JOIN to improve this request
$cmd[] = array("id" => $key, "nbr" => $nbr, "prix_unit" => $res['prix']);
$prix_total += $res['prix'] * $nbr;
$bNbr = ($nbrStock - $nbr);
$stmt->bindParam(':id', $key);
$stmt->bindParam(':nbr', $bNbr);
$stmt->execute();
}
// DEBUG !!!!
// DEBUG !!!!
$_SESSION['id_user'] = 42;
// DEBUG !!!!
// DEBUG !!!!
$id_user = $_SESSION['id_user'];
$date = date("Y-m-d H:i:s");
$stmt = $bdd->prepare("INSERT INTO ticket
(`list`, `prix_total`, `date`, id_user)
VALUES (:list, :prix_total, :date, :id_user)");
$stmt->bindParam(':list', json_encode($cmd));
$stmt->bindParam(':prix_total', $prix_total);
$stmt->bindParam(':date', $date);
$stmt->bindParam(':id_user', $id_user);
$stmt->execute();
echo json_encode($cmd);
$_SESSION['list'] = Array();
header('Location: index.php?page=client');
?>