Skip to content

Commit

Permalink
Feat: CRUD finalizado.
Browse files Browse the repository at this point in the history
  • Loading branch information
natan-xav2019 committed Nov 9, 2023
1 parent 6848b3e commit cbbdb8e
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 47 deletions.
10 changes: 1 addition & 9 deletions assets/css/style.css
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
:root {

}

body {
margin: 5px 0;
}

h3 {
table {
text-align: center;
}

#from-create {

}
10 changes: 10 additions & 0 deletions assets/function/formatAge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

function formatAge(int $age) {
if($age == 1) {
return "$age ano";
} elseif($age > 1 || $age == 0){
return "$age anos";
}
}
?>
51 changes: 42 additions & 9 deletions class/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,70 @@ class Connection implements Crud{
private $connected;

public function __construct() {
$this->open();

if ($this->connected->connect_error) {
die("Conexão falhou: " . $this->connected->connect_error);
}

$this->close();
}

function open() {
$this->connected = mysqli_connect($this->server ,$this->user ,$this->password ,$this->dbname);
}

function close() {
$this->connected->close;
$this->connected->close();
}

#Função destinada para criar um novo registro no banco.
function create() {
function create(array $data) {
$this->open();

$name = $data["name"];
$age = $data["age"];

$sql = "INSERT INTO `consumer`(`name`, `age`) VALUES ('$name','$age')";

$this->connected->query($sql);

$this->close();
}
#Função destinada a fazer uma busca no banco e apresentar ela
function read() {
function read(string $condicional) {
$this->open();

$sql = "SELECT * FROM `consumer` WHERE $condicional";

$array = $this->connected->query($sql);

$this->close();

return $array;
}

#função destinada a eliminar um registro do banco.
function delete(int $id) {

$this->open();

$sql = "DELETE FROM `consumer` WHERE id = $id";
$this->connected->query($sql);

$this->close();
}

#função destinada a editar um registro do banco.
function update(int $id, array $request) {

}


function update(int $id, array $user) {
$this->open();

$name = $user["name"];
$age = $user["age"];

$sql = "UPDATE `consumer` SET `name` = '$name', `age` = '$age' WHERE id = $id";
$this->connected->query($sql);

$this->close();
}

}
6 changes: 3 additions & 3 deletions class/Crud.PHP
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

interface Crud {
#Função destinada para criar um novo registro no banco.
public function create();
public function create(array $data);

#Função destinada a fazer uma busca no banco e apresentar ela
public function read();
public function read(string $condicional);

#função destinada a eliminar um registro do banco.
public function delete(int $id);

#função destinada a editar um registro do banco.
#função destinada a editar um registro do banco.
public function update(int $id, array $request);


Expand Down
59 changes: 59 additions & 0 deletions data/consumer.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
-- phpMyAdmin SQL Dump
-- version 5.2.0
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Nov 09, 2023 at 09:04 PM
-- Server version: 10.4.24-MariaDB
-- PHP Version: 8.1.6

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `crud`
--

-- --------------------------------------------------------

--
-- Table structure for table `consumer`
--

CREATE TABLE `consumer` (
`id` int(255) NOT NULL,
`name` varchar(100) NOT NULL,
`age` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `consumer`
--
ALTER TABLE `consumer`
ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `consumer`
--
ALTER TABLE `consumer`
MODIFY `id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
87 changes: 61 additions & 26 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
<?php
include "class/Crud.php";
include "class/Connection.php";
include "assets/function/formatAge.php";

$conn = new Connection();

if(isset($_POST["create"],$_POST["name"],$_POST["age"])) {
$user = [
"name" => $_POST["name"],
"age" => $_POST["age"],
];

$conn->create($user);
}

if(isset($_POST["delele"])) {
$conn->delete($_POST["id"]);
}

if(isset($_POST["update"])) {
$id = $_POST['id'];
header("Location: updateUser.php?id=$id");
}

$users = $conn->read(True);
?>
<!DOCTYPE html>
<html lang="pt-br">

Expand All @@ -13,17 +40,16 @@
</head>

<body>
<header>
<h1>CRUD o que e?</h1>
<header class="container">
<h1 class="text-center">CRUD o que e?</h1>
</header>

<div class="container">
<main>
<div class="text-center">
<h2>CRUD</h2>
</div>
<div>
<div class="row">
<p>CRUD e uma sigla para falar das 4 operações básicas de um banco de dados, Insert, Select, Update e
Delete.</p>

Expand All @@ -34,7 +60,6 @@
<li>D - DELETE</li>
</ul>
</div>
<div class="row">
<div class="col">
<h3>CREATE</h3>
Expand All @@ -55,42 +80,52 @@
</div>

<div>
<form class="row" id="from-create" action="" method="post">
<form class="row" id="from-create" action="index.php" method="post">
<div class="form-group text-center">
<h1>Insira os dados para aparecer na tabela</h1>
</div>
<div class="form-group">
<label class="" for="name">Nome</label>
<input class="form-control" placeholder="digite o nome" id="name" type="text">
<label for="name">Nome</label>
<input class="form-control" placeholder="digite o nome" id="name" name="name" type="text" required>
</div>
<div class="form-group">
<label for="age">Idade</label>
<input class="form-control" placeholder="digite a idade" id="age" type="number">
<input class="form-control" placeholder="digite a idade" id="age" name="age" type="number" required>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Enviar">
<div class="form-group mt-2">
<input class="btn btn-primary" type="submit" name="create" value="Enviar">
</div>
</form>
</div>

<table class="table">
<tr>
<th>Nome</th>
<th>Idade</th>
<th>Comandos</th>
</tr>
<tr>
<td>Natan</td>
<td>23 anos</td>
<td>
<button>Editar</button>
<button>Deletar</button>
</td>
</tr>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Nome</th>
<th scope="col">Idade</th>
<th scope="col">Comandos</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user):?>
<tr>
<td><?=$user["name"]; ?></td>
<td><?= formatAge($user["age"]);?></td>
<td>

<form action="index.php" id="form-delete" method="post">
<input type='hidden' name='id' value=<?= $user["id"] ?>>
<button type="submit" name="update" class="btn btn-outline-primary">Editar</button>
<button type="submit" name="delele" class="btn btn-danger">Deletar</button>
</form>
</td>
</tr>
<?php endforeach?>
</tbody>
</table>
</main>

<footer>
<footer class="footer">
<p>feito por Natan Xavier.</p>
</footer>
</div>
Expand Down
Loading

0 comments on commit cbbdb8e

Please sign in to comment.