Advertisement
Ivan_sjc

Meu PHp

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