Advertisement
Gourmet

construct

Apr 3rd, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.02 KB | None | 0 0
  1.  public function __construct($pdo, $id) {
  2.     $this->pdo = $pdo;
  3.     $this->id = $id;
  4.              
  5.     $query_select = 'SELECT * FROM ficha_alunos WHERE id = :id';
  6.  
  7.     try {
  8.         $query_select_result = $this->pdo->prepare($query_select);
  9.         $query_select_result->bindParam(':id', $this->id, PDO::PARAM_INT);
  10.         $query_select_result->execute();
  11.         while($query_show_result = $query_select_result->FETCH(PDO::FETCH_OBJ)) {
  12.             $this->id = $query_show_result->id;
  13.             $this->num = $query_show_result->num;
  14.             $this->name = $query_show_result->nome;
  15.             $this->box  = $query_show_result->caixa;
  16.         }  
  17.      } catch(PDOException $e){
  18.                   return 'Erro: '.$e->getMessage();
  19.      }  
  20.  
  21. }
  22.  
  23.  
  24. // método update
  25. public function update() {
  26.     $update = 'UPDATE ficha_alunos SET num = :num, nome = :nome, caixa = :caixa WHERE id = :id';
  27.  
  28.     try {
  29.         $query_update_result = $this->pdo->prepare($update);
  30.         $query_update_result->bindParam(':id', $this->id, PDO::PARAM_INT);
  31.         $query_update_result->bindParam(':num', $this->num, PDO::PARAM_STR);
  32.         $query_update_result->bindParam(':nome', $this->name, PDO::PARAM_STR);
  33.         $query_update_result->bindParam(':caixa', $this->box, PDO::PARAM_STR);
  34.         $query_update_result->execute();
  35.         $count = $query_update_result->rowCount();
  36.  
  37.         if($count > 0){
  38.             return '<div class="alert alert-success mb-0">
  39.                         <button type="button" class="close" data-dismiss="alert">×</button>
  40.                         <strong>Sucesso!</strong> Os dados foram atualizados.
  41.                   </div>';
  42.         } else {
  43.             return '<div class="alert alert-danger mb-0">
  44.                         <button type="button" class="close" data-dismiss="alert">×</button>
  45.                        <strong>Erro ao atualizar!</strong> Não foi possível atualizar os dados.
  46.                   </div>';
  47.  
  48.         }
  49.     } catch(PDOException $e){
  50.         return 'Erro: '. $e->getMessage();
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement