Created
August 25, 2025 18:22
-
-
Save uchoamaster/794a3db0d0240a5ef83ea05dacb7f947 to your computer and use it in GitHub Desktop.
CRUD com salvamento de imagem no banco de dados com 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
| 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 VARCHAR(255) 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 include 'db.php'; | |
| if ($_SERVER["REQUEST_METHOD"] == "POST") { | |
| $nome = $_POST['nome']; | |
| $preco = $_POST['preco']; | |
| $img_nome = $_FILES['imagem']['name']; | |
| $tmp = $_FILES['imagem']['tmp_name']; | |
| $destino = 'uploads/' . $img_nome; | |
| move_uploaded_file($tmp, $destino); | |
| $sql = "INSERT INTO produtos (nome, preco, imagem) VALUES ('$nome', '$preco', '$img_nome')"; | |
| $conn->query($sql); | |
| header("Location: index.php"); | |
| exit; | |
| } | |
| ?> | |
| <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 | |
| $conn = new mysqli("localhost", "root", "", "crud_php"); | |
| if ($conn->connect_error) { | |
| die("Erro: " . $conn->connect_error); | |
| } | |
| ?> |
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 include 'db.php'; | |
| $id = $_GET['id']; | |
| $res = $conn->query("SELECT imagem FROM produtos WHERE id=$id"); | |
| $row = $res->fetch_assoc(); | |
| unlink("uploads/" . $row['imagem']); | |
| $conn->query("DELETE FROM produtos WHERE id=$id"); | |
| header("Location: index.php"); | |
| exit; | |
| ?> |
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 include 'db.php'; ?> | |
| <!DOCTYPE html> | |
| <html> | |
| <head><title>CRUD com Imagem</title></head> | |
| <body> | |
| <h2>Lista de Produtos</h2> | |
| <a href="create.php">Adicionar Novo</a> | |
| <table border="1" cellpadding="10"> | |
| <tr> | |
| <th>ID</th> | |
| <th>Nome</th> | |
| <th>Preço</th> | |
| <th>Imagem</th> | |
| <th>Ações</th> | |
| </tr> | |
| <?php | |
| $res = $conn->query("SELECT * FROM produtos"); | |
| while($row = $res->fetch_assoc()): | |
| ?> | |
| <tr> | |
| <td><?= $row['id'] ?></td> | |
| <td><?= $row['nome'] ?></td> | |
| <td>R$ <?= number_format($row['preco'], 2, ',', '.') ?></td> | |
| <td><img src="uploads/<?= $row['imagem'] ?>" width="100"></td> | |
| <td> | |
| <a href="update.php?id=<?= $row['id'] ?>">Editar</a> | | |
| <a href="delete.php?id=<?= $row['id'] ?>" onclick="return confirm('Excluir?')">Excluir</a> | |
| </td> | |
| </tr> | |
| <?php endwhile; ?> | |
| </table> | |
| </body> | |
| </html> |
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 include 'db.php'; | |
| $id = $_GET['id']; | |
| $res = $conn->query("SELECT * FROM produtos WHERE id=$id"); | |
| $produto = $res->fetch_assoc(); | |
| if ($_SERVER["REQUEST_METHOD"] == "POST") { | |
| $nome = $_POST['nome']; | |
| $preco = $_POST['preco']; | |
| if (!empty($_FILES['imagem']['name'])) { | |
| $img_nome = $_FILES['imagem']['name']; | |
| $tmp = $_FILES['imagem']['tmp_name']; | |
| move_uploaded_file($tmp, "uploads/" . $img_nome); | |
| $conn->query("UPDATE produtos SET nome='$nome', preco='$preco', imagem='$img_nome' WHERE id=$id"); | |
| } else { | |
| $conn->query("UPDATE produtos SET nome='$nome', preco='$preco' WHERE id=$id"); | |
| } | |
| header("Location: index.php"); | |
| exit; | |
| } | |
| ?> | |
| <form method="POST" enctype="multipart/form-data"> | |
| Nome: <input type="text" name="nome" value="<?= $produto['nome'] ?>"><br> | |
| Preço: <input type="text" name="preco" value="<?= $produto['preco'] ?>"><br> | |
| Imagem Atual: <img src="uploads/<?= $produto['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