CrudAval

Download do Código
read.php (Apenas Leitura)
<?php
	require_once("funcoes.php");
	$resultado = instrucao($conn,"SELECT * FROM alunos ORDER BY id DESC");
?>
update.php (Apenas Leitura)
<?php
	require_once("funcoes.php");
	// Editar
	if (isset($_GET['editar'])) {
		$id = intval($_GET['editar']);
		$busca = instrucao($conn,"SELECT * FROM alunos WHERE id = $id");
		if ($busca && $busca->num_rows > 0) {
			$dados	= $busca->fetch_assoc();
			$id		= $dados['id'];
			$nome	= $dados['nome'];
			$email	= $dados['email'];
		}
	}
	
	// Atualizar
	if (isset($_GET['bAtualizar'])) {
		if (!empty($id) && !empty($nome) && !empty($email)) {
			$sql = "UPDATE alunos SET nome = '$nome', email = '$email' WHERE id = $id";
			if (instrucao($conn,$sql)) {
				$mensagem = "Aluno atualizado com sucesso!";
				//$id    = "";
				//$nome  = "";
				//$email = "";
			} else {
				$mensagem = "Erro ao atualizar registro.";
			}
		} else {
			$mensagem = "Erro ao atualizar registro.";
		}
	}
?>
index.php (Apenas Leitura)
<?php
	require_once 'conexao.php';
	require_once 'read.php';
	require_once 'create.php';
	require_once 'update.php';
	require_once 'delete.php';
	
	$pagina = $_SERVER["PHP_SELF"];

	$id			= "";
	$nome		= "";
	$email		= "";
	$mensagem	= "";

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


?>
<!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)) { ?>
				<div class="alerta"><?php echo $mensagem; ?></div>
			<?php } ?>

			<form method="GET">
				<div class="campo">
					<label for="txtId2">Id</label>
					<input type="number" id="txtId2" value="<?php echo $id; ?>" disabled placeholder="(Automático)">
					<input type="hidden" name="hId" value="<?php echo $id; ?>">
				</div>

				<div class="campo">
					<label for="txtNome">Nome do Aluno</label>
					<input type="text" id="txtNome" name="txtNome" value="<?php echo $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 $email; ?>" required placeholder="exemplo@dominio.com">
				</div>

				<div class="botoes">
					<?php if (!empty($id)) { ?>
						<input type="submit" value="Salvar Alterações" name="bAtualizar" class="btn btn-atualizar">
						<a href="<?php echo $pagina;?>" class="btn btn-cancelar">Cancelar</a>
					<?php } else { ?>
						<input type="submit" value="Cadastrar Aluno" name="bCadastrar" class="btn btn-cadastrar">
					<?php } ?>
				</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
					if ($resultado && $resultado->num_rows > 0) {
						while ($aluno = $resultado->fetch_assoc()) {
							echo "<tr>" .
									"<td>" . $aluno['id']    . "</td>" .
									"<td>" . $aluno['nome']  . "</td>" .
									"<td>" . $aluno['email'] . "</td>" .
									"<td style='text-align: center;' class='acoes'>" .
									"<a href='$pagina?editar="  . $aluno['id'] . "' class='btn btn-editar'>Editar</a> " .
									"<a href='$pagina?excluir=" . $aluno['id'] . "' class='btn btn-excluir'>Excluir</a>" .
									"</td>" .
								 "</tr>";
						}
					} else {
						echo '<tr><td colspan="4" style="text-align: center; color: #888;">Nenhum aluno cadastrado.</td></tr>';
					}
					mysqli_close($conn);
					?>
				</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;
}
conexao.php (Apenas Leitura)
<?php
	mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
	
	$servidor = "localhost";
	$usuario  = "root";
	$senha    = "";
	$banco    = "etecvav";

	$conn  = mysqli_connect($servidor, $usuario, $senha, $db);

	if (!$conn) {
		die("Falha na conexão: " . mysqli_connect_error());
	}
?>
funcoes.php (Apenas Leitura)
<?php
require_once("conexao.php");

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);
}

function instrucao($conn, $sql) {
    $resultado = mysqli_query($conn, $sql);
    return $resultado;
}
?>
delete.php (Apenas Leitura)
<?php
	require_once("funcoes.php");
	// Apagar
	if (isset($_GET['excluir'])) {
		$sql = "DELETE FROM alunos WHERE id = $id";
		if (instrucao($conn,$sql)) {
			$mensagem = "Aluno removido com sucesso!";
		} else {
			$mensagem = "Erro ao excluir registro.";
		}
	}
?>
create.php (Apenas Leitura)
<?php
	require_once("funcoes.php");
	// Cadastrar
	if (isset($_GET['bCadastrar'])) {
		if (!empty($nome) && !empty($email)) {
			$sql = "INSERT INTO alunos (nome, email) VALUES ('$nome', '$email')";
			if (instrucao($conn,$sql)) {
				$mensagem	= "Aluno cadastrado com sucesso!";
			} else {
				$mensagem	= "Erro ao cadastrar aluno.";
			}
		} else {
			$mensagem		= "Preencha todos os campos!";
		}
	}
?>
Atualizar Visualização