CrudOOAval

Download do Código
Crud.php (Apenas Leitura)
<?php
class Crud {
	public $id       = '';
	public $nome     = '';
	public $email    = '';
	public $conn     = null;
	public $mensagem = '';
	public $pagina   = "";

	function __construct($conn) {
		$this->conn   = $conn;
		$this->pagina = $_SERVER['PHP_SELF'];
	}
	public function criar (): string {
		if (empty($this->nome) || empty($this->email))
			return "Preencha todos os campos!";

		$sql = "INSERT INTO alunos (nome, email) VALUES ('"
			. $this->nome  . "', '"
			. $this->email . "')";
		
		if ($this->conn->query($sql)) {
			$id    = '';
			$nome  = '';
			$email = '';
			return "Aluno cadastrado com sucesso!";
		} else {
			return "Erro ao cadastrar aluno.";
		}
	}

	public function ler():string {
		$retorno = '';

		$sql = "SELECT * FROM alunos ORDER BY id DESC";
		$resultado = $this->conn->query($sql);

		if ($resultado && $resultado->num_rows > 0) {
			while ($aluno = $resultado->fetch_assoc()) {
				$retorno .= "<tr>";
				$retorno .= "   <td>" . $aluno['id']    . "</td>";
				$retorno .= "   <td>" . $aluno['nome']  . "</td>";
				$retorno .= "   <td>" . $aluno['email'] . "</td>";
				$retorno .= "   <td style='text-align: center;' class='acoes'>";
				$retorno .= "      <a href='" . $this->pagina . "?editar="  . $aluno['id'] . "' class='btn btn-editar'>Editar</a> ";
				$retorno .= "      <a href='" . $this->pagina . "?excluir=" . $aluno['id'] . "' class='btn btn-excluir'>Excluir</a>";
				$retorno .= "   </td>";
				$retorno .= "</tr>";
			}
		} else {
			$retorno = '<tr><td colspan="4" style="text-align: center; color: #888;">Nenhum aluno cadastrado.</td></tr>';
		}
		return $retorno;
	}

	public function atualizar (): bool {
		$sql = "UPDATE alunos SET "
			. "   nome     ='" . $this->nome  . "',"
			. "   email    ='" . $this->email . "' "
			. " WHERE "
			. "   id       =" . $this->id;
			
		return ($this->conn->query($sql));
	}

	public function apagar (): bool {
		$sql = "DELETE FROM alunos WHERE id = " . $this->id;

		return ($this->conn->query($sql));
	}

	public function editar($id):bool {
		$this->id    = $id;
		$this->nome  = '';
		$this->email = '';

		$sql = "SELECT * FROM alunos WHERE id = $id";
		$resultado = $this->conn->query($sql);

		if ($resultado && $resultado->num_rows > 0) {
			$aluno = $resultado->fetch_assoc();
			$this->nome  = $aluno['nome'];
			$this->email = $aluno['email'];
			return true;
		} else {
			return false;
		}
	}
}
?>
index.php (Apenas Leitura)
<?php
	require_once 'Conexao.php';
	require_once 'Crud.php';
	require_once 'Util.php';

	$conexao = new Conexao();
	$conn    = $conn->getConn();
	$crud    = new Crud($conn);

	$crud->id       = 0;
	$crud->nome     = '';
	$crud->email    = '';
	$crud->mensagem = '';

	if (isset($_GET['hId']))        $crud->id     = intval($_GET['hId']);
	if (isset($_GET['editar']))     $crud->id     = intval($_GET['editar']);
	if (isset($_GET['excluir']))    $crud->id     = intval($_GET['excluir']);
	if (isset($_GET['txtNome']))    $crud->nome   = trim($_GET['txtNome']);
	if (isset($_GET['txtEmail']))   $crud->email  = trim($_GET['txtEmail']);

	if (isset($_GET['bCadastrar'])) $mensagem = $crud->criar(); 
	if (isset($_GET['bAtualizar'])) $mensagem = $crud->atualizar(); 
	if (isset($_GET['incluir']))    $mensagem = "Aluno cadastrado com sucesso!";
	if (isset($_GET['editar']))     $crud->editar($_GET['editar']);
	if (isset($_GET['excluir']))    $mensagem = $crud->apagar();
?>
<!DOCTYPE html>
<html lang="pt-br">
	<head>
		<meta charset="utf-8">
		<title>Cadastro de Alunos</title>
		<link rel="stylesheet" href="style.css">
	</head>
	<body>
		<div class="container">
			<h1>Cadastro de Alunos</h1>
			<?php
				if (!empty($mensagem)) {
					echo "<div class='alerta'>$mensagem</div>";
				}
			?>
			<form method="GET">
				<div class="campo">
					<label for="txtId2">Id</label>
					<input type="number" id="txtId2" value="<?php echo $crud->id; ?>" disabled placeholder="(Automático)">
					<input type="hidden" name="hId"  value="<?php echo $crud->id; ?>">
				</div>

				<div class="campo">
					<label for="txtNome">Nome do Aluno</label>
					<input type="text" id="txtNome" name="txtNome" value="<?php echo $crud->nome; ?>" required placeholder="Digite o nome completo">
				</div>

				<div class="campo">
					<label for="txtEmail">E-mail</label>
					<input type="email" id="txtEmail" name="txtEmail" value="<?php echo $crud->email; ?>" required placeholder="exemplo@dominio.com">
				</div>

				<div class="botoes">
					<?php
						if (!empty($crud->id)) { 
							echo "<input type='submit' value='Salvar Alterações' name='bAtualizar' class='btn btn-atualizar'>";
							echo "<a href='". $crud->pagina . "' class='btn btn-cancelar'>Cancelar</a>";
						} else {
							echo "<input type='submit' value='Cadastrar Aluno' name='bCadastrar' class='btn btn-cadastrar'>";
						}
					?>
				</div>
			</form>

			<table>
				<thead>
					<tr>
						<th style="width: 10%;">Id</th>
						<th style="width: 40%;">Nome</th>
						<th style="width: 35%;">E-mail</th>
						<th style="width: 15%; text-align: center;">Ações</th>
					</tr>
				</thead>
				<tbody>
					<?php
						echo $crud->ler('');
						$conn->close();
					?>
				</tbody>
			</table>
		</div>
	</body>
</html>
estilo.css
* {
	box-sizing: border-box;
	margin: 0;
	padding: 0;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

body {
	background-color: #f4f6f9;
	color: #333;
	padding: 30px 15px;
}

.container {
	margin: 0 auto;
	background: #ffffff;
	padding: 25px;
	border-radius: 8px;
	box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

h1 {
	font-size: 22px;
	margin-bottom: 20px;
	color: #1f2937;
	text-align: center;
}

.alerta {
	padding: 10px 14px;
	background-color: #e0f2fe;
	color: #0369a1;
	border-left: 4px solid #0284c7;
	margin-bottom: 20px;
	border-radius: 4px;
	font-size: 14px;
}

form {
	display: flex;
	flex-direction: column;
	gap: 12px;
	margin-bottom: 30px;
}

.campo {
	display: flex;
	flex-direction: column;
	gap: 4px;
}

label {
	font-size: 13px;
	font-weight: 600;
	color: #4b5563;
}

input[type="text"],
input[type="email"],
input[type="number"] {
	padding: 10px;
	border: 1px solid #d1d5db;
	border-radius: 5px;
	font-size: 14px;
	background-color: #f9fafb;
}

input[type="text"]:focus,
input[type="email"]:focus {
	border-color: #3b82f6;
	outline: none;
	background-color: #fff;
}

input:disabled {
	background-color: #e5e7eb;
	cursor: not-allowed;
}

.botoes {
	display: flex;
	gap: 10px;
	margin-top: 8px;
}

.btn {
	padding: 10px 16px;
	border: none;
	border-radius: 5px;
	font-weight: 600;
	font-size: 14px;
	cursor: pointer;
	text-decoration: none;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	transition: opacity 0.2s;
}

.btn:hover {
	opacity: 0.9;
}

.btn-cadastrar {
	background-color: #10b981;
	color: white;
	flex: 1;
}

.btn-atualizar {
	background-color: #3b82f6;
	color: white;
	flex: 1;
}

.btn-cancelar {
	background-color: #6b7280;
	color: white;
	padding: 10px 16px;
}

table {
	width: 100%;
	border-collapse: collapse;
	margin-top: 10px;
	background-color: #fff;
}

th, td {
	padding: 12px 14px;
	text-align: left;
	border-bottom: 1px solid #e5e7eb;
	font-size: 14px;
}

th {
	background-color: #f8fafc;
	color: #475569;
	font-weight: 600;
}

tr:hover {
	background-color: #f8fafc;
}

.acoes {
	display: flex;
	gap: 8px;
}

.btn-editar {
	background-color: #3b82f6;
	color: white;
	padding: 4px 8px;
	font-size: 12px;
	border-radius: 4px;
}

.btn-excluir {
	background-color: #ef4444;
	color: white;
	padding: 4px 8px;
	font-size: 12px;
	border-radius: 4px;
}
Util.php (Apenas Leitura)
<?php
class Util {
	public function formatarNome(string $nome): string {
		$nome = mb_convert_case(trim($nome), MB_CASE_TITLE, "UTF-8");

		$procurar   = [' Da ', ' De ', ' Di ', ' Do ', ' Du ', ' Das ', ' Des ', ' Dis ', ' Dos ', ' E ', ' O '];
		$substituir = [' da ', ' de ', ' di ', ' do ', ' du ', ' das ', ' des ', ' dis ', ' dos ', ' e ', ' o ' ];
	
		return str_replace($procurar, $substituir, $nome);
	}

	public function instrucao($conn, $sql) {
		$resultado = mysqli_query($conn, $sql);
		return $resultado;
	}
}
?>
Conexao.php (Apenas Leitura)
<?php
class Conexao {
	private $host = 'localhost';
	private $user = 'root';
	private $pass = '';
	private $db   = 'etecvav';
	private $conn = null;

	function __construct() {
		$this->conn = new mysqli($this->host, $this->user, $this->pass, $this->db);
	}

	public function getConn() {
		return $this->conn;
	}

	public function fechar() {
		$this->connection->close();
	}
}
?>
Atualizar Visualização