<?php // initialise une variable $pdo connecté à la base locale require_once("initPDO.php"); class User{ private $id; private $name; private $email; // public function __construct($props = array()) { $this−>props = $props; } // public function __get($prop) { return $this−>props[$prop]; } // public function __set($prop, $val) { $this−>props[$prop] = $val; } private static function getAllUsers(){ global $pdo; $request = $pdo->prepare("select * from users"); $request->execute(); $allUsers = $request->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class() ); $pdo = null; return $allUsers; } public static function showAllUsersAsTable(){ $allUsers = static::getAllUsers(); $result = '<table><tr><td>Id</td><td>Nom</td><td>Email</td></tr>'; foreach($allUsers as $user){ $result.= $user->toHtml(); } $result .= "</table>"; echo $result; } private function toHtml(){ return('<tr><td>'.$this->id.'</td><td>'.$this->name.'</td><td>'.$this->email.'</td></tr>'); } } // POST if(isset($_POST['name']) && isset($_POST['email'])) { // ajout en POST $addRequest = $pdo->prepare("INSERT INTO users(id, name, email) VALUES (NULL, '".$_POST["name"]."','".$_POST["email"]."')"); $addRequest->execute(); //SQL } // echo $allUsers; /*** close the database connection ***/ ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> table { border-top: 1px solid black; border-bottom: 1px solid black; } td { text-align: center; padding-left: 2em; padding-right: 2em; } </style> </head> <body> <h1>Users</h1> <?php User::showAllUsersAsTable(); ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <p>name : <input type="text" name="name" /></p> <p>email : <input type="text" name="email" /></p> <input type="submit" value="Add"> </form> </body> </html>