Created
August 25, 2025 18:32
-
-
Save uchoamaster/5a843046ba0927f31d658da9c50d0944 to your computer and use it in GitHub Desktop.
Crud pdo orientado a objetos com imagem como base64 BLOB no banco
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE DATABASE crud_php; | |
| USE crud_php; | |
| CREATE TABLE produtos ( | |
| id INT AUTO_INCREMENT PRIMARY KEY, | |
| nome VARCHAR(100) NOT NULL, | |
| preco DECIMAL(10,2) NOT NULL, | |
| imagem LONGBLOB NOT NULL | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once '../config/Database.php'; | |
| require_once '../models/Produto.php'; | |
| $db = (new Database())->connect(); | |
| $produto = new Produto($db); | |
| if ($_SERVER["REQUEST_METHOD"] === "POST") { | |
| $produto->create($_POST['nome'], $_POST['preco'], $_FILES['imagem']['tmp_name']); | |
| header("Location: index.php"); | |
| } | |
| ?> | |
| <form method="POST" enctype="multipart/form-data"> | |
| Nome: <input type="text" name="nome"><br> | |
| Preço: <input type="text" name="preco"><br> | |
| Imagem: <input type="file" name="imagem"><br> | |
| <input type="submit" value="Salvar"> | |
| </form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // config/Database.php | |
| class Database { | |
| private $host = 'localhost'; | |
| private $db = 'crud_php'; | |
| private $user = 'root'; | |
| private $pass = ''; | |
| public $conn; | |
| public function connect() { | |
| $this->conn = null; | |
| try { | |
| $this->conn = new PDO( | |
| "mysql:host={$this->host};dbname={$this->db}", | |
| $this->user, $this->pass | |
| ); | |
| $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
| } catch (PDOException $e) { | |
| die("Erro: " . $e->getMessage()); | |
| } | |
| return $this->conn; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once 'config/Database.php'; | |
| require_once 'models/Produto.php'; | |
| $db = (new Database())->connect(); | |
| $produto = new Produto($db); | |
| $produto->delete($_GET['id']); | |
| header("Location: views/index.php"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once '../config/Database.php'; | |
| require_once '../models/Produto.php'; | |
| $db = (new Database())->connect(); | |
| $produto = new Produto($db); | |
| $produtos = $produto->all(); | |
| ?> | |
| <a href="create.php">Novo Produto</a> | |
| <table border="1"> | |
| <tr> | |
| <th>ID</th> | |
| <th>Nome</th> | |
| <th>Preço</th> | |
| <th>Imagem</th> | |
| <th>Ações</th> | |
| </tr> | |
| <?php foreach ($produtos as $p): ?> | |
| <tr> | |
| <td><?= $p['id'] ?></td> | |
| <td><?= $p['nome'] ?></td> | |
| <td>R$ <?= number_format($p['preco'], 2, ',', '.') ?></td> | |
| <td><img src="data:image/jpeg;base64,<?= base64_encode($p['imagem']) ?>" width="100"></td> | |
| <td> | |
| <a href="update.php?id=<?= $p['id'] ?>">Editar</a> | |
| <a href="../delete.php?id=<?= $p['id'] ?>" onclick="return confirm('Tem certeza?')">Excluir</a> | |
| </td> | |
| </tr> | |
| <?php endforeach; ?> | |
| </table> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // models/Produto.php | |
| class Produto { | |
| private $conn; | |
| private $table = "produtos"; | |
| public function __construct($db) { | |
| $this->conn = $db; | |
| } | |
| // Inserir produto | |
| public function create($nome, $preco, $imagemTmp) { | |
| $imgBin = file_get_contents($imagemTmp); | |
| $sql = "INSERT INTO {$this->table} (nome, preco, imagem) VALUES (:nome, :preco, :imagem)"; | |
| $stmt = $this->conn->prepare($sql); | |
| return $stmt->execute([ | |
| ':nome' => $nome, | |
| ':preco' => $preco, | |
| ':imagem' => $imgBin | |
| ]); | |
| } | |
| // Listar todos | |
| public function all() { | |
| $stmt = $this->conn->query("SELECT id, nome, preco, imagem FROM {$this->table}"); | |
| return $stmt->fetchAll(PDO::FETCH_ASSOC); | |
| } | |
| // Buscar por ID | |
| public function find($id) { | |
| $stmt = $this->conn->prepare("SELECT * FROM {$this->table} WHERE id = :id"); | |
| $stmt->execute([':id' => $id]); | |
| return $stmt->fetch(PDO::FETCH_ASSOC); | |
| } | |
| // Atualizar produto | |
| public function update($id, $nome, $preco, $imagemTmp = null) { | |
| if ($imagemTmp) { | |
| $imgBin = file_get_contents($imagemTmp); | |
| $sql = "UPDATE {$this->table} SET nome = :nome, preco = :preco, imagem = :imagem WHERE id = :id"; | |
| $params = [':nome' => $nome, ':preco' => $preco, ':imagem' => $imgBin, ':id' => $id]; | |
| } else { | |
| $sql = "UPDATE {$this->table} SET nome = :nome, preco = :preco WHERE id = :id"; | |
| $params = [':nome' => $nome, ':preco' => $preco, ':id' => $id]; | |
| } | |
| $stmt = $this->conn->prepare($sql); | |
| return $stmt->execute($params); | |
| } | |
| // Deletar | |
| public function delete($id) { | |
| $stmt = $this->conn->prepare("DELETE FROM {$this->table} WHERE id = :id"); | |
| return $stmt->execute([':id' => $id]); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once '../config/Database.php'; | |
| require_once '../models/Produto.php'; | |
| $db = (new Database())->connect(); | |
| $produto = new Produto($db); | |
| $p = $produto->find($_GET['id']); | |
| if ($_SERVER["REQUEST_METHOD"] === "POST") { | |
| $imgTmp = !empty($_FILES['imagem']['tmp_name']) ? $_FILES['imagem']['tmp_name'] : null; | |
| $produto->update($_GET['id'], $_POST['nome'], $_POST['preco'], $imgTmp); | |
| header("Location: index.php"); | |
| } | |
| ?> | |
| <form method="POST" enctype="multipart/form-data"> | |
| Nome: <input type="text" name="nome" value="<?= $p['nome'] ?>"><br> | |
| Preço: <input type="text" name="preco" value="<?= $p['preco'] ?>"><br> | |
| <img src="data:image/jpeg;base64,<?= base64_encode($p['imagem']) ?>" width="100"><br> | |
| Nova imagem: <input type="file" name="imagem"><br> | |
| <input type="submit" value="Atualizar"> | |
| </form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment