Advertisement
Ivan_sjc

PHP Orientado a Objetos Crud

Jan 23rd, 2015
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.45 KB | None | 0 0
  1. <?php
  2.    
  3.    
  4.     class Database
  5. {
  6.  
  7.     // specify your own database credentials
  8.     private $host = "127.0.0.1";
  9.     private $db_name = "sisim";
  10.     private $username = "root";
  11.     private $password = "";
  12.     public $conn;
  13.  
  14.     // get the database connection
  15.     public function getConnection()
  16.     {
  17.  
  18.         $this->conn = null;
  19.  
  20.         try {
  21.             $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
  22.         } catch (PDOException $exception) {
  23.             echo "Connection error: " . $exception->getMessage();
  24.         }
  25.  
  26.         return $this->conn;
  27.     }
  28. }
  29.  
  30.  
  31.  
  32.  
  33.  
  34.     class Empresa{
  35.  
  36.     //base de dados e nome da tabela
  37.  
  38.     private $conn;
  39.     private $table_name = "cadastro_emp";
  40.  
  41.     //propriendades da Classe
  42.  
  43.     public $razao_social;
  44.     public $cnpj;
  45.     public $inscricao_estadual;
  46.  
  47.  
  48.     public function __construct($db)
  49.     {
  50.         $this->conn = $db;
  51.     }
  52.  
  53.     // create product
  54.     public function create()
  55.     {
  56.         //write query
  57.         $query = "INSERT INTO " . $this->table_name . " (`razao_social`,`cnpj`,`inscricao_estadual`) VALUES (?,?,?)";
  58.  
  59.         $stmt = $this->conn->prepare($query);
  60.  
  61.         $stmt->bindParam(1, $this->razao_social);
  62.         $stmt->bindParam(2, $this->cnpj);
  63.         $stmt->bindParam(3, $this->inscricao_estadual);
  64.  
  65.         if ($stmt->execute()) {
  66.             return true;
  67.         } else {
  68.             return false;
  69.         }
  70.  
  71.     }
  72. }
  73.    
  74.    
  75.    
  76.    
  77.     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  78.         $database = new Database();
  79.         $empresa = new Empresa($database->getConnection());
  80.  
  81.         // set Empresa property values
  82.         $empresa-> razao_social = $_POST['razaosocial'];
  83.         $empresa-> cnpj = $_POST['cnpj'];
  84.         $empresa-> inscricao_estadual = $_POST['inscricaoestadual'];
  85.  
  86.         // create the Empresa
  87.     if ($empresa->create()) {
  88.         echo "<div class=\"alert alert-success alert-dismissable\">";
  89.         echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
  90.         echo "Empresa Cadastrada.";
  91.         echo "</div>";
  92.     } // if unable to create the Empresa, tell the user
  93.     else {
  94.         echo "<div class=\"alert alert-danger alert-dismissable\">";
  95.         echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
  96.         echo "Erro ao Cadastrar.";
  97.         echo "</div>";
  98.     }
  99. }
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement