Advertisement
Alessandro_O

cliente.php

Apr 17th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.99 KB | None | 0 0
  1. <?php
  2.  
  3.     require_once 'connection.php';
  4.     $con = new connection();
  5.  
  6.     class cliente{
  7.  
  8.         private $nome;
  9.         private $email;
  10.         private $senha;
  11.         private $cpf;
  12.  
  13.         function cliente($nome,$email,$senha,$cpf){
  14.  
  15.             $this->nome = $nome;
  16.             $this->email = $email;
  17.             $this->senha = $senha;
  18.             $this->cpf = $cpf;
  19.  
  20.         }
  21.         //CRUD stands for Create, Read, Update and Delete
  22.  
  23.         //The function below create a new data for our database
  24.  
  25.         public function getNome(){
  26.             return $this->nome;
  27.         }
  28.  
  29.         public function register(){
  30.  
  31.             try{
  32.                
  33.  
  34.                 $stmt = $con->prepare("INSERT INTO clientes (cpf,nome,email,senha) VALUES (?,?,?,?)");
  35.                 $stmt->execute([$this->cpf,$this->nome,$this->emial,$this->senha]);
  36.             }catch(PDOExcepition $e){
  37.                 echo 'ERROR: '.$e->getMessage();
  38.             }
  39.         }
  40.         //this function show data that is in our data base
  41.         public function showData(){
  42.  
  43.             try{
  44.                 $stmt = $con->prepare("SELECT nome,cpf,email FROM clientes");
  45.                 while ($data = $stmt->fetch()) {
  46.                     echo "Nome: ".$data['nome']. "<br>CPF: ".$data['cpf']."<br>E-mail: ".$data['email'];
  47.                 }
  48.  
  49.             }catch(PDOExcepition $e){
  50.                 echo 'ERROR: '.$e->getMessage();
  51.             }
  52.         }
  53.         /*this function will update data inside the database. In that case I'll create a switch statement to choose between the places to be updated.
  54.         */
  55.  
  56.         public function upDateData($option,$cpf,$nome,$email,$senha){
  57.  
  58.             switch ($option) {
  59.                 case 1:
  60.                     try{
  61.                         $stmt = $con->prepare("UPDATE clientes SET nome = ? WHERE cpf = ?");
  62.                         $stmt->execute([$nome,$cpf]);
  63.  
  64.                     }catch(PDOExcepition $e){
  65.                         echo 'ERROR: '.$e->getMessage();
  66.                     }
  67.                     break;
  68.                 case 2:
  69.                     try{
  70.                         $stmt = $con->prepare("UPDATE clientes SET email = ? WHERE cpf = ?");
  71.                         $stmt->execute([$email,$cpf]);
  72.  
  73.                     }catch(PDOExcepition $e){
  74.                         echo 'ERROR: '.$e->getMessage();
  75.                     }
  76.                     break;
  77.                 case 3:
  78.                     try{
  79.                         $stmt = $con->oreoare("UPDATE clientes SET senha = ? WHERE cpf = ?");
  80.                         $stmt->execute([$senha,$cpf]);
  81.  
  82.                     }catch(PDOExcepition $e){
  83.                         echo 'ERROR: '.$e->getMessage();
  84.                     }
  85.                     break;
  86.                 case 4:
  87.                     try{
  88.                         $stmt = $con->prepare("UPDATE clienes SET cpf = ? WHERE cpf = ?");
  89.                         $stmt->execute([$cpf,$cpf]);
  90.  
  91.                     }catch(PDOExcepition $e){
  92.                         echo 'ERROR: '.$e->getMessage();
  93.                     }
  94.                     break;
  95.                 case 5:
  96.                     try{
  97.                         $stmt= $con->prepare("UPDATE clientes set cpf = ?,nome= ?, email = ?, senha = ? WHERE cpf = ?");
  98.                         $stmt->execute([$cpf,$nome,$email,$senha,$cpf]);
  99.  
  100.                     }catch(PDOExcepition $e){
  101.                         echo 'ERROR: '.$e->getMessage();
  102.                     }
  103.                     break;
  104.                
  105.                 default:
  106.                     echo "Choose something to be updated!";
  107.                     break;
  108.             }
  109.         }
  110.             //this is the last function of CRUD, it'll be responsible for delete the specific data from our database.
  111.         public function deleteData($cpf){
  112.  
  113.             try{
  114.                 $stmt = $con->prepare("DELETE FROM clientes WHERE cpf = ?");
  115.                 $stmt->execute([$cpf]);
  116.            
  117.             }catch(PDOExcepition $e){
  118.                     echo 'ERROR: '.$e->getMessage();
  119.                 }
  120.         }
  121. }
  122.  
  123.  
  124.  
  125. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement