Advertisement
Ivan_sjc

Crud

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