Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class BD{
- private static $conexiune_bd = NULL;
- public static function obtine_conexiune(){
- if (is_null(self::$conexiune_bd))
- {
- self::$conexiune_bd = new PDO('mysql:host=localhost;dbname=sezatoare', 'cosmin', 'cosmin');
- }
- return self::$conexiune_bd;
- }
- }
- class Actiune{
- public function adaugaMesaj($utilizator, $mesaj){ //create
- $sql = "INSERT INTO mesaje (utilizator, mesaj, created_at, updated_at) VALUES (:utilizator, :mesaj, :created_at, :updated_at)";
- $cerere = BD::obtine_conexiune()->prepare($sql);
- return $cerere -> execute ([
- 'utilizator' => $utilizator,
- 'mesaj' => $mesaj,
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- }
- public function obtineMesaje(){ // read
- $sql = "SELECT * FROM mesaje";
- $cerere = BD::obtine_conexiune()->prepare($sql);
- $cerere->execute();
- return $cerere->fetchAll();
- }
- public function modificaMesaj($id, $mesaj){ // update
- $sql = "UPDATE mesaje SET mesaj = :mesaj, updated_at = :updated_at WHERE id = :id";
- $cerere = BD::obtine_conexiune()->prepare($sql);
- return $cerere->execute([
- 'mesaj' => $mesaj,
- 'updated_at' => date('Y-m-d H:i:s'),
- 'id' => $id
- ]);
- }
- public function stergeMesaj($id){
- $sql = "DELETE FROM mesaje WHERE id = ?";
- $cerere = BD::obtine_conexiune()->prepare($sql);
- return $cerere-> execute([$id]);
- }
- }
- $actiune = new Actiune();
- echo "<h1>Inainte de Inserare:</h1><br />";
- echo "<pre>";
- print_r ($actiune -> obtineMesaje());
- echo "</pre>";
- $actiune -> adaugaMesaj('Nicolae','Mesaj initial, ce va fi modificat.');
- echo "<h1>Dupa inserare:</h1><br />";
- echo "<pre>";
- $mesaje = $actiune -> obtineMesaje();
- print_r ($mesaje);
- echo "</pre>";
- $id_mesaj_inserat = $mesaje[count($mesaje) -1]["id"];
- $actiune -> modificaMesaj($id_mesaj_inserat,'Acesta este mesajul modificat');
- echo "<h1>Dupa modificare:</h1><br />";
- echo "<pre>";
- $mesaje = $actiune -> obtineMesaje();
- print_r ($mesaje);
- echo "</pre>";
- $actiune -> stergeMesaj($id_mesaj_inserat);
- echo "<h1>Dupa Stergere:</h1><br />";
- echo "<pre>";
- $mesaje = $actiune -> obtineMesaje();
- print_r ($mesaje);
- echo "</pre>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement