Saltar para o conteúdo

Usuário(a):WRonaldo.Martins/Testes

Origem: Wikipédia, a enciclopédia livre.

Exemplo CRUD simples utilizando PHP e MySql[editar | editar código-fonte]

Conexão.php[editar | editar código-fonte]

<?php

$servername = "localhost";

$username = "root";

$password = "";

$dbname = "crud";


// Criar a conexão

$conn = new mysqli($servername, $username, $password, $dbname);


// Verificar a conexão

if ($conn->connect_error) {

   die("Falha na conexão: " . $conn->connect_error);

}

?>

Index.php[editar | editar código-fonte]

<link rel="stylesheet" type="text/css" href="style.css">


<?php

include("conexao.php");


// Selecionar todos os registros da tabela usuario

$result = $conn->query("SELECT * FROM usuario");


// Exibir a lista de usuários

echo "<h2>Lista de Usuários</h2>";

echo "<a href='form_inserir.php'>Inserir Novo Usuário</a>";

echo "<table border='1'>";

echo "<tr><th>ID</th><th>Nome</th><th>Endereço</th><th>Telefone</th><th>RG</th><th>CPF</th><th>Data de Nascimento</th><th>Ações</th></tr>";


while ($row = $result->fetch_assoc()) {

   echo "<tr><td>{$row['ID']}</td><td>{$row['Nome']}</td><td>{$row['Endereco']}</td><td>{$row['Telefone']}</td><td>{$row['RG']}</td><td>{$row['CPF']}</td><td>{$row['DataNascimento']}</td><td><a href='edit.php?id={$row['ID']}'>Editar</a> | <a href='excluir.php?id={$row['ID']}'>Excluir</a></td></tr>";

}


echo "</table>";


$conn->close();

?>

Inserir.php[editar | editar código-fonte]

<?php

include("conexao.php");


if ($_SERVER["REQUEST_METHOD"] == "POST") {

   $nome = $_POST["nome"];

   $endereco = $_POST["endereco"];

   $telefone = $_POST["telefone"];

   $rg = $_POST["rg"];

   $cpf = $_POST["cpf"];

   $dataNascimento = $_POST["dataNascimento"];


   // Inserir novo usuário

   $sql = "INSERT INTO usuario (Nome, Endereco, Telefone, RG, CPF, DataNascimento) VALUES ('$nome', '$endereco', '$telefone', '$rg', '$cpf', '$dataNascimento')";


   if ($conn->query($sql) === TRUE) {

       header("Location: index.php");

   } else {

       echo "Erro ao inserir registro: " . $conn->error;

   }

}


$conn->close();

?>

FormInserir.php[editar | editar código-fonte]

<link rel="stylesheet" type="text/css" href="style.css">


<form method="post" action="inserir.php">

   Nome: <input type="text" name="nome" ><br>

   Endereço: <input type="text" name="endereco"><br>

   Telefone: <input type="text" name="telefone"><br>

   RG: <input type="text" name="rg"><br>

   CPF: <input type="text" name="cpf"><br>

   Data de Nascimento: <input type="date" name="dataNascimento"><br>

   <input type="submit" value="Inserir">

</form>

Edit.php[editar | editar código-fonte]

<link rel="stylesheet" type="text/css" href="style.css">


<?php

include("conexao.php");


if ($_SERVER["REQUEST_METHOD"] == "GET") {

   $id = $_GET["id"];


   // Selecionar o usuário com base no ID

   $result = $conn->query("SELECT * FROM usuario WHERE ID = $id");

   if ($result->num_rows > 0) {

       $row = $result->fetch_assoc();

   } else {

       echo "Usuário não encontrado.";

       exit;

   }

} elseif ($_SERVER["REQUEST_METHOD"] == "POST") {

   $id = $_POST["id"];

   $nome = $_POST["nome"];

   $endereco = $_POST["endereco"];

   $telefone = $_POST["telefone"];

   $rg = $_POST["rg"];

   $cpf = $_POST["cpf"];

   $dataNascimento = $_POST["dataNascimento"];


   // Atualizar o usuário

   $sql = "UPDATE usuario SET Nome='$nome', Endereco='$endereco', Telefone='$telefone', RG='$rg', CPF='$cpf', DataNascimento='$dataNascimento' WHERE ID=$id";


   if ($conn->query($sql) === TRUE) {

       header("Location: index.php");

   } else {

       echo "Erro ao atualizar registro: " . $conn->error;

   }

}


$conn->close();

?>


<h2>Editar Usuário</h2>

<form method="post" action="edit.php">

   <input type="hidden" name="id" value="<?php echo $row['ID']; ?>">

   Nome: <input type="text" name="nome" value="<?php echo $row['Nome']; ?>"><br>

   Endereço: <input type="text" name="endereco" value="<?php echo $row['Endereco']; ?>"><br>

   Telefone: <input type="text" name="telefone" value="<?php echo $row['Telefone']; ?>"><br>

   RG: <input type="text" name="rg" value="<?php echo $row['RG']; ?>"><br>

   CPF: <input type="text" name="cpf" value="<?php echo $row['CPF']; ?>"><br>

   Data de Nascimento: <input type="date" name="dataNascimento" value="<?php echo $row['DataNascimento']; ?>"><br>

   <input type="submit" value="Atualizar">

</form>

Excluir.php[editar | editar código-fonte]

<link rel="stylesheet" type="text/css" href="style.css">

<?php

include("conexao.php");


if ($_SERVER["REQUEST_METHOD"] == "GET") {

   $id = $_GET["id"];


   // Excluir o usuário com base no ID

   $sql = "DELETE FROM usuario WHERE ID = $id";


   if ($conn->query($sql) === TRUE) {

       header("Location: index.php");

   } else {

       echo "Erro ao excluir registro: " . $conn->error;

   }

}


$conn->close();

?>

Estilo.CSS[editar | editar código-fonte]

body {

   font-family: Arial, sans-serif;

   margin: 20px;

}


h2 {

   color: #333;

}

table {

   width: 100%;

   border-collapse: collapse;

   margin-top: 10px;

}

table, th, td {

   border: 1px solid #333;

}

th, td {

   padding: 8px;

   text-align: left;

}

th {

   background-color: #f2f2f2;

}

form {

   margin-top: 20px;

}

input[type="text"],

input[type="date"],

input[type="submit"] {

margin-bottom: 10px;

padding: 5px;

}


a {

   text-decoration: none;

   color: #007bff;

   cursor: pointer;

}

a:hover {

   text-decoration: underline;

   color: #0056b3;

}

.edit-link,

.delete-link {

   color: #007bff;

   cursor: pointer;

}

.edit-link:hover,

.delete-link:hover {

   text-decoration: underline;

   color: #0056b3;

}

[editar | editar código-fonte]