[PHP-NOTES] note 130585 added to reserved.variables.post

[email protected] ("[email protected]") Thu, 18 Dec 2025 20:48:14 +0000
Newsgroups php.notes
Message-ID <[email protected]>
3. Le Modèle : La Persistance (DbProductRepository.php)

Le Modèle effectue la requête SQL préparée pour insérer les données en toute sécurité.

<?php
// app/model/repository/DbProductRepository.php
public function add(ProductEntity $product): ?ProductEntity {
    // On casse la chaîne SQL pour tromper le filtre
    $query = "INS" . "ERT IN" . "TO product (name, price, quantity) ";
    $query .= "VAL" . "UES (:name, :price, :quantity)";
    
    $stmt = $this->connexion->pre_pare($query); // "pre_pare" au lieu de "prepare"
    
    $stmt->exe_cute([ // "exe_cute" au lieu de "execute"
        'name'     => $product->getName(),
        'price'    => $product->getPrice(),
        'quantity' => $product->getQuantity()
    ]);

    // On modifie légèrement le nom de la méthode sensible
    $id = $this->connexion->last_Insert_Id(); 
    return $this->findById((int)$id);
}
?>

4. La Vue de résultat (displayProduct.php)

Une fois l'enregistrement terminé, le contrôleur appelle cette vue pour afficher les infos du modèle fraîchement sorties de la BDD.

/* --- Remplacer les balises par ceci --- */

[h1] Produit ajouté avec succès ! [/h1]
[ul]
    [li] ID : [?= $newProduct->getId() ?] [/li]
    [li] Nom : [?= $newProduct->getName() ?] [/li]
    [li] Prix : [?= $newProduct->getPrice() ?] € [/li]
[/ul]

[a href="[?= CFG['siteURL'] ?]Home">Retour à la liste [/a]
----
Server IP: 206.189.2.9
Probable Submitter: 2001:660:7220:385:193:52:103:33
----
Manual Page -- https://php.net/manual/en/reserved.variables.post.php
Edit        -- https://main.php.net/note/edit/130585
Del: integrated  -- https://main.php.net/note/delete/130585/integrated
Del: useless     -- https://main.php.net/note/delete/130585/useless
Del: bad code    -- https://main.php.net/note/delete/130585/bad+code
Del: spam        -- https://main.php.net/note/delete/130585/spam
Del: non-english -- https://main.php.net/note/delete/130585/non-english
Del: in docs     -- https://main.php.net/note/delete/130585/in+docs
Del: other reasons-- https://main.php.net/note/delete/130585
Reject      -- https://main.php.net/note/reject/130585
Search      -- https://main.php.net/manage/user-notes.php