creazione metodo che restituisce l'id del prodotto creato (fix delle doppie connessioni) (parte 131)
| | |
| | | ]; |
| | | |
| | | if($this->model->insert($data)) { |
| | | echo "record saved"; |
| | | echo "record saved, ID:" . $this->model->getInsertID(); |
| | | } else { |
| | | echo $this->viewer->render("shared/header.php", [ |
| | | "title" => "New Product" |
| | |
| | | use PDO; |
| | | |
| | | class Database { |
| | | |
| | | private ?PDO $pdo = null; |
| | | public function __construct(private string $host, |
| | | private string $name, |
| | | private string $user, |
| | |
| | | |
| | | } |
| | | public function getConnection(): PDO { |
| | | $dns = "mysql:host={$this->host};dbname={$this->name};charset=utf8;port=3306"; |
| | | return new PDO($dns, $this->user, $this->password, [ |
| | | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
| | | ]); |
| | | if ($this->pdo === null) { |
| | | $dns = "mysql:host={$this->host};dbname={$this->name};charset=utf8;port=3306"; |
| | | $this->pdo = new PDO($dns, $this->user, $this->password, [ |
| | | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
| | | ]); |
| | | } |
| | | |
| | | return $this->pdo; |
| | | |
| | | } |
| | | } |
| | |
| | | $parts = explode("\\", $this::class); |
| | | return strtolower(array_pop($parts)); |
| | | } |
| | | public function getInsertID(): string { |
| | | $conn = $this->database->getConnection(); |
| | | return $conn->lastInsertId(); |
| | | } |
| | | public function findAll(): array|bool |
| | | { |
| | | $pdo = $this->database->getConnection(); |