Skip to content

Instantly share code, notes, and snippets.

@silvadias22
Last active August 23, 2019 18:43
Show Gist options
  • Save silvadias22/7868e647506abb80f3e8634517373abb to your computer and use it in GitHub Desktop.
Save silvadias22/7868e647506abb80f3e8634517373abb to your computer and use it in GitHub Desktop.
Crud.php
<!-- CRUD - ISUD - by: Fabrício da Silva Dias
____________________________________________________________
|| ||
|| crud = [C]reate, [R]ead, [U]pdate e [D]elete ou Destroy ||
|| isud = [I]NSERT, [S]ELECT, [U]pdate, [D]elete ||
||__________________________________________________________||
-->
<?php
function conexaoPDO(){
#Iniciando conexão com banco de dados
$pdo = new PDO ('mysql:host=127.0.0.1;dbname=Familia','root', '');
$pdo->setattribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
?>
<!-- CRUD - ISUD - by: Fabrício da Silva Dias
____________________________________________________________
|| ||
|| crud = [C]reate, [R]ead, [U]pdate e [D]elete ou Destroy ||
|| isud = [I]NSERT, [S]ELECT, [U]pdate, [D]elete ||
||__________________________________________________________||
-->
<?php
include "database.php";
$pdo = conexaoPDO();
$id = $_GET['id'];
if ($id){
$stmt = $pdo->prepare(
"delete from Familia where id =:id"
);
$stmt->bindValue(":id", $id);
$stmt->execute();
header("Location: registros.php");
$pdo = null;
}else{
$stmt = $pdo->prepare(
"delete from Familia"
);
$stmt->execute();
header("Location: registros.php");
$pdo = null;
};
?>
DROP DATABASE Familia;
CREATE DATABASE Familia;
USE Familia;
CREATE TABLE Familia (
id int(3) NOT NULL PRIMARY KEY auto_increment,
nome varchar(30) NOT NULL,
idade int(3) NOT NULL,
sexo char(1) NOT NULL
);
<!-- CRUD - ISUD - by: Fabrício da Silva Dias
____________________________________________________________
|| ||
|| crud = [C]reate, [R]ead, [U]pdate e [D]elete ou Destroy ||
|| isud = [I]NSERT, [S]ELECT, [U]pdate, [D]elete ||
||__________________________________________________________||
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf8"><!-- declarar formato de teclado -->
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<!--
Definindo as configurações da div conteúdo (Onde estará o painel).
O padrão de colunas da grid do bootstrap é de 12 colunas.
Com essa base, vamos definir a div com 4 colunas e afastar ela das laterais em 4 colunas por lado.
-->
<div class="col-md-4 col-md-offset-4" id="conteudo">
<div class="panel panel-primary">
<div class="panel-heading"><!-- Cabeçalho do painel -->
<h3 class="panel-title">Formulario</h3>
</div>
<div class="panel-body"> <!-- Corpo do painel -->
<form action="registrar.php" name="registrar" method="get" onsubmit="return validarCadastro();">
<div class="form-group">
<span>Nome:</span>
<input class="form-control input-sm" name="nome" type="text">
<span>Idade:</span>
<input class="form-control input-sm" name="idade" type="text">
<span>Sexo:</span>
<select class="form-control input-sm" name="sexo">
<option selected="selected">M</option>
<option>F</option>
</select>
<input value="Enviar" class="btn btn-success btn-sm" type="submit">
<a href="registros.php" id="exibirRegistros" class="btn btn-primary btn-sm">Exibir registros</a>
</div> <!-- Div Form-Group -->
</form>
</div> <!-- Panel-Body -->
</div> <!-- Panel -->
</div> <!-- Div Conteúdo -->
<script language="javascript">
function validarCadastro(){
if(document.registrar.nome.value.trim()==""){
alert("Por favor, preencha o campo nome!");
document.registrar.nome.focus();
return false;
}
if(document.registrar.idade.value.trim()==""){
alert("Por favor, preencha o campo idade!");
document.registrar.idade.focus();
return false;
}
}
</script>
</body>
</html>
<!DOCTYPE html>
<!-- CRUD - ISUD - by: Fabrício da Silva Dias
____________________________________________________________
|| ||
|| crud = [C]reate, [R]ead, [U]pdate e [D]elete ou Destroy ||
|| isud = [I]NSERT, [S]ELECT, [U]pdate, [D]elete ||
||__________________________________________________________||
-->
<?php
include "database.php";
$id = $_GET['id'];
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf8"><!-- declarar formato de teclado -->
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="col-md-4 col-md-offset-4" id="conteudo">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Formulario</h3>
</div>
<div class="panel-body">
<form action="update.php" name="registrar" method="get" onsubmit="return validarCadastro();">
<div class="form-group">
<input type='hidden' name='id' value='<?php echo $_GET['id']; ?>'>
<span>Nome:</span>
<input class="form-control input-sm" name="nome" type="text" id="nome">
<span>Idade:</span>
<input class="form-control input-sm" name="idade" type="text" id="idade">
<span>Sexo:</span>
<select class="form-control input-sm" name="sexo" id="sexo">
<option selected="selected">M</option>
<option>F</option>
</select>
<input value="Enviar" class="btn btn-success btn-sm" type="submit">
<a href="registros.php" id="exibirRegistros" class="btn btn-primary btn-sm">Exibir registros</a>
</div> <!-- Div Form-Group -->
</form>
</div> <!-- Panel-Body -->
</div> <!-- Panel -->
</div> <!-- Div Conteúdo -->
<script language="javascript">
function validarCadastro(){
if(document.registrar.nome.value.trim()==""){
alert("Por favor, preencha o campo nome!");
document.registrar.nome.focus();
return false;
}
if(document.registrar.idade.value.trim()==""){
alert("Por favor, preencha o campo idade!");
document.registrar.idade.focus();
return false;
}
}
</script>
</body>
</html>
<!-- CRUD - ISUD - by: Fabrício da Silva Dias
____________________________________________________________
|| ||
|| crud = [C]reate, [R]ead, [U]pdate e [D]elete ou Destroy ||
|| isud = [I]NSERT, [S]ELECT, [U]pdate, [D]elete ||
||__________________________________________________________||
-->
<?php
include "database.php";
$pdo = conexaoPDO();
$nome = $_GET["nome"];
$idade = $_GET["idade"];
$sexo = $_GET["sexo"];
$stmt = $pdo->prepare(
"insert into Familia(
nome,
idade,
sexo
)values(
:nome,
:idade,
:sexo
);"
);
$stmt->bindValue(":nome", $nome);
$stmt->bindValue(":idade", $idade);
$stmt->bindValue(":sexo", $sexo);
$stmt->execute();
$pdo = null;
header("Location: registros.php");
?>
<!-- CRUD - ISUD - by: Fabrício da Silva Dias
____________________________________________________________
|| ||
|| crud = [C]reate, [R]ead, [U]pdate e [D]elete ou Destroy ||
|| isud = [I]NSERT, [S]ELECT, [U]pdate, [D]elete ||
||__________________________________________________________||
-->
<html>
<?php
#Incluindo um enredo(script) externo 'database.php'.
include "database.php";
$pdo = conexaoPDO();
$stmt = $pdo->prepare("select * from Familia;");
$stmt->execute();
$pdo = null;
?>
<head>
<meta charset="utf8">
</head>
<body>
<!-- Estabelecendo configuração de localização (usando bootstrap) e definindo id da div. -->
<div class="col-md-6 col-md-offset-3" id="tabelaLocal">
<!-- criando a tabela com configurações do bootstrap, nomeando e adicionando id -->
<table class="table table-bordered table-striped table-condensed" id="registro" name="registrar">
<!-- Criando a cabeça da tabela e adicionando a identificação (id) -->
<tr class="text-center" id="tr1">
<td>nome </td>
<td>idade</td>
<td>sexo</td>
<td></td>
<td></td>
</tr>
<?php
#Criando o laço de repetição e Atribuindo os valores da variável $stmt na variável $result.
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)){
?>
<!-- Criando o corpo da tabela dentro do laço de repetição (while) -->
<tr class="text-center">
<!-- Exibindo os registros específicos do db nas colunas da tabela.
(Obs: Os valores já foram declarado e atribuido na variável $result)
-->
<td><?php print_r ($result['nome']); ?></td>
<td><?php print_r ($result['idade']); ?></td>
<td><?php print_r ($result['sexo']); ?></td>
<td><a href="reform.php?id=<?php echo $result['id'];?>" class="btn btn-success btn-sm"> Editar</a></td>
<td><a href="deletar.php?id=<?php echo $result['id'];?>" class="btn btn-danger btn-sm"> Excluir</a></td>
</tr>
<?php
}; #Fechando laço de repetição.
?>
</table>
<a href="index.php" class="btn btn-Primary btn-sm col-md-offset-5"> Voltar </a>
<a href="deletar.php?id=" class="btn btn-danger btn-sm"> Excluir tudo</a>
</div>
</body>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/style.css">
</html>
<!-- CRUD - ISUD - by: Fabrício da Silva Dias
____________________________________________________________
|| ||
|| crud = [C]reate, [R]ead, [U]pdate e [D]elete ou Destroy ||
|| isud = [I]NSERT, [S]ELECT, [U]pdate, [D]elete ||
||__________________________________________________________||
-->
<?php
include "database.php";
$pdo = conexaoPDO();
$id = $_GET["id"];
$nome = $_GET["nome"];
$idade = $_GET["idade"];
$sexo = $_GET["sexo"];
$stmt = $pdo->prepare(
'update Familia '.
'set nome = :nome, '.
'idade = :idade, '.
'sexo = :sexo ' .
'where id = :id');
$stmt->bindValue(":id", $id);
$stmt->bindValue(":nome", $nome);
$stmt->bindValue(":idade", $idade);
$stmt->bindValue(":sexo", $sexo);
$stmt->execute();
$pdo = null;
header("Location: registros.php");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment