Skip to content

Commit

Permalink
Mise à jour
Browse files Browse the repository at this point in the history
  • Loading branch information
NouvelleTechno committed Sep 14, 2019
1 parent 53031eb commit 208f059
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
68 changes: 47 additions & 21 deletions models/Produits.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ public function __construct($db){
* @return void
*/
public function lire(){
// On écrit la requête
$sql = "SELECT c.nom as categories_nom, p.id, p.nom, p.description, p.prix, p.categories_id, p.created_at FROM " . $this->table . " p LEFT JOIN categories c ON p.categories_id = c.id ORDER BY p.created_at DESC";


// On prépare la requête
$query = $this->connexion->prepare($sql);

// On exécute la requête
$query->execute();


// On retourne le résultat
return $query;
}

Expand All @@ -43,21 +47,28 @@ public function lire(){
* @return void
*/
public function creer(){

$sql = "INSERT INTO " . $this->table . " SET nom=:nom, prix=:prix, description=:description, categories_id=:categories_id";


// Ecriture de la requête SQL en y insérant le nom de la table
$sql = "INSERT INTO " . $this->table . " SET nom=:nom, prix=:prix, description=:description, categories_id=:categories_id, created_at=:created_at";

// Préparation de la requête
$query = $this->connexion->prepare($sql);


// Protection contre les injections
$this->nom=htmlspecialchars(strip_tags($this->nom));
$this->prix=htmlspecialchars(strip_tags($this->prix));
$this->description=htmlspecialchars(strip_tags($this->description));
$this->categories_id=htmlspecialchars(strip_tags($this->categories_id));

$this->created_at=htmlspecialchars(strip_tags($this->created_at));

// Ajout des données protégées
$query->bindParam(":nom", $this->nom);
$query->bindParam(":prix", $this->prix);
$query->bindParam(":description", $this->description);
$query->bindParam(":categories_id", $this->categories_id);

$query->bindParam(":created_at", $this->created_at);

// Exécution de la requête
if($query->execute()){
return true;
}
Expand All @@ -70,17 +81,21 @@ public function creer(){
* @return void
*/
public function lireUn(){
// On écrit la requête
$sql = "SELECT c.nom as categories_nom, p.id, p.nom, p.description, p.prix, p.categories_id, p.created_at FROM " . $this->table . " p LEFT JOIN categories c ON p.categories_id = c.id WHERE p.id = ? LIMIT 0,1";


// On prépare la requête
$query = $this->connexion->prepare( $sql );


// On attache l'id
$query->bindParam(1, $this->id);


// On exécute la requête
$query->execute();

// on récupère la ligne
$row = $query->fetch(PDO::FETCH_ASSOC);

// On hydrate l'objet
$this->nom = $row['nom'];
$this->prix = $row['prix'];
Expand All @@ -95,18 +110,23 @@ public function lireUn(){
* @return void
*/
public function supprimer(){
// On écrit la requête
$sql = "DELETE FROM " . $this->table . " WHERE id = ?";


// On prépare la requête
$query = $this->connexion->prepare( $sql );


// On sécurise les données
$this->id=htmlspecialchars(strip_tags($this->id));

// On attache l'id
$query->bindParam(1, $this->id);


// On exécute la requête
if($query->execute()){
return true;
}

return false;
}

Expand All @@ -116,26 +136,32 @@ public function supprimer(){
* @return void
*/
public function modifier(){
// On écrit la requête
$sql = "UPDATE " . $this->table . " SET nom = :nom, prix = :prix, description = :description, categories_id = :categories_id WHERE id = :id";


// On prépare la requête
$query = $this->connexion->prepare($sql);


// On sécurise les données
$this->nom=htmlspecialchars(strip_tags($this->nom));
$this->prix=htmlspecialchars(strip_tags($this->prix));
$this->description=htmlspecialchars(strip_tags($this->description));
$this->categories_id=htmlspecialchars(strip_tags($this->categories_id));
$this->id=htmlspecialchars(strip_tags($this->id));


// On attache les variables
$query->bindParam(':nom', $this->nom);
$query->bindParam(':prix', $this->prix);
$query->bindParam(':description', $this->description);
$query->bindParam(':categories_id', $this->categories_id);
$query->bindParam(':id', $this->id);


// On exécute
if($query->execute()){
return true;
}

return false;
}

}
2 changes: 1 addition & 1 deletion produits/lire_un.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// On récupère le produit
$produit->lireUn();

// On vérifie si on a au moins 1 produit
// On vérifie si le produit existe
if($produit->nom != null){

$prod = [
Expand Down

0 comments on commit 208f059

Please sign in to comment.