Skip to content

Instantly share code, notes, and snippets.

@guilhermeof
Created June 10, 2016 00:20
Show Gist options
  • Save guilhermeof/9bbfa9e187c9409d7cfa4baaf75cded3 to your computer and use it in GitHub Desktop.
Save guilhermeof/9bbfa9e187c9409d7cfa4baaf75cded3 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Cadastro</title>
<meta charset="utf-8">
</head>
<body>
<form method="post" action="processa_form.php">
Nome do Produto <input type="text" name="product">
Descrição <input type="text" name="description">
Preço <input type="number" name="price">
<input type="submit" value="send">
</form>
</body>
</html>
<?php
namespace Insert;
class InsertProduct extends Product
{
private $user = "root";
private $pass = "root";
private $dsn = "mysql:dbname=formacao_php;host=localhost";
public function insert(Product $inserir){
$pdo = new PDO($dsn,$user,$pass);
$sql = "INSERT INTO produtos(nome, descricao, preco) values ( :nome, :descricao, :preco)";
$insert=$pdo->prepare($sql);
$insert->bindvalue(":nome", $inserir->getProduct(), PDO::PARAM_STR);
$insert->bindvalue(":descricao", $inserir->getDescription(), PDO::PARAM_STR);
$insert->bindvalue(":preco", $inserir->getPrice(), PDO::PARAM_STR);
if($insert->execute()){
echo "Registro inserido com sucesso com o número de ID ".$pdo->lastInsertId();
}
else{
echo "Erro!";
}
}
}
<?php
chdir(dirname(__DIR__));
require 'vendor/autoload.php';
use Produto\Product;
use Insert\InsertProduct;
$product = $_POST["product"];
$description = $_POST["description"];
$price = $_POST["price"];
$prod = new Product();
$prod->setProduct($product);
$prod->setDescription($description);
$prod->setPrice($price);
$insert = new InsertProduct();
$insert->insert($prod);
<?php
namespace Produto;
class Product
{
private $product;
private $description;
private $price;
public function getProduct()
{
return $this->product;
}
public function setProduct($product)
{
$this->product = $product;
}
public function getDescription()
{
return $this->description;
}
public function setDescription($description)
{
$this->description = $description;
}
public function getPrice()
{
return $this->price;
}
public function setPrice($price)
{
$this->price = $price;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment