Commit 8b6d9c2d authored by Vytor Calixto's avatar Vytor Calixto 👾

CRUD completo

parent c4ce1b11
<?php
$hostname = "localhost";
$username = "root";
$password = "";
mysql_connect("$hostname", "$username", "$password") or die(mysql_error());
mysql_select_db("dbCrud") or die(mysql_error());
?>
\ No newline at end of file
<?php
require('DbConnection.php');
require_once('DbConnection.php');
if (empty($_POST['id'])) {
if (empty($_POST['id'])) {
salvarUsuario(formToArray());
} else {
atualizarUsuario(formToArray());
......@@ -12,32 +12,45 @@ if (empty($_POST['id'])) {
function salvarUsuario($user) {
$values = implode(',', $user);
echo "<pre>";
print_r($values);
echo "</pre>";
$sql = "INSERT INTO tbUser (nome,email,senha) values ({$values}) ";
$sql = "INSERT INTO tbUser (nome,login,email,senha) values ({$values}) ";
$result = mysql_query($sql);
if (!$result) {
echo 'ERRO ao se cadastrar';
exit;
}else{
header("Location:list.php");
exit;
}
}
function atulizarUsuario($user) {
function atualizarUsuario($user) {
$values = implode(',', $user);
echo "pre" + print_r($values) + "</pre>";
//$sql = "UPDATE SET WHERE"
$sql = "UPDATE tbUser SET nome=".$user['nome'].",login=".$user['login'].","
. "email=".$user['email']." WHERE id=".$user['id'];
$result = mysql_query($sql);
if (!$result) {
echo 'ERRO ao atualizar';
exit;
}else{
header("Location:list.php");
exit;
}
}
function formToArray() {
$user = array();
$user['nome'] = isset($_POST['nome']) ? $_POST['nome'] : "";
$user['email'] = isset($_POST['email']) ? $_POST['nome'] : "";
$user['senha'] = isset($_POST['senha']) ? $_POST['senha'] : "";
$user['id'] = isset($_POST['id']) ? $_POST['id'] : "";
$user['nome'] = isset($_POST['nome']) ? "'".$_POST['nome']."'" : "";
$user['login'] = isset($_POST['login']) ? "'".$_POST['login']."'" : "";
$user['email'] = isset($_POST['email']) ? "'".$_POST['email']."'" : "";
$user['senha'] = isset($_POST['senha']) ? $_POST['senha']: "";
return $user;
}
......
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<?php
require_once('DbConnection.php');
$id = "";
$name = null;
$login = null;
$email = null;
if (!empty($_GET['id'])) {
$id = $_GET['id'];
$sql = "Select * FROM tbUser where id=" . $id;
$result = mysql_query($sql);
if (!$result) {
echo "ERRO ao recuperar usuário";
} else {
$user = mysql_fetch_array($result);
$nome = $user['nome'];
$login = $user['login'];
$email = $user['email'];
}
} else {
$nome = "";
$login = "";
$email = "";
}
?>
<!DOCTYPE html>
<html>
<head>
......@@ -12,14 +34,15 @@ and open the template in the editor.
</head>
<body>
<div class="center-div">
<form method="post" class="pure-form pure-form-stacked">
<form method="post" action="UserController.php" class="pure-form pure-form-stacked">
<fieldset>
<input type="text" name="nome" placeholder="Nome"/>
<input type="text" name="email" placeholder="Email" />
<input type="text" name="nome" placeholder="Nome" value="<?php echo!empty($nome) ? $nome : ""; ?>"/>
<input type="text" name="login" placeholder="Login" value="<?php echo!empty($login) ? $login : ""; ?>"/>
<input type="text" name="email" placeholder="Email" value="<?php echo!empty($email) ? $email : ""; ?>"/>
<input type="password" name="senha" placeholder="Senha"/>
<input type="password" name="confirmarSenha" placeholder="Confirme a senha"/>
<input type="submit" value="Cadastrar" class="pure-button pure-button-primary"/>
<input type="hidden" name="id" value=""/>
<input type="hidden" name="id" value="<?php echo!empty($id) ? $id : ""; ?>"/>
</fieldset>
</form>
</div>
......
......@@ -31,24 +31,23 @@ and open the template in the editor.
</tr>
</thead>
<tr>
<td>Jill</td>
<td>email@email.com</td>
<td>jill2310</td>
<td>
<a href="cadastro.php" class="pure-button">Atualizar</a>
<a href="cadastro.php" class="pure-button">Remover</a>
</td>
</tr>
<tr>
<td>Eve</td>
<td>email2@email.com</td>
<td>eve10</td>
<td>
<a href="cadastro.php" class="pure-button">Atualizar</a>
<a href="cadastro.php" class="pure-button">Remover</a>
</td>
</tr>
<?php
include_once('DbConnection.php');
$sql = "SELECT * FROM tbUser";
$result = mysql_query($sql);
while($user = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>' . $user['nome'] . '</td>';
echo '<td>' . $user['email'] . '</td>';
echo '<td>' . $user['login'] . '</td>';
echo '<td>
<a href="cadastro.php?id='.$user['id'].'" class="pure-button button-success">Atualizar</a>
<a href="remover.php?id='.$user['id'].'" class="pure-button button-error">Remover</a>
</td>';
echo '</tr>';
}
?>
</table>
</body>
</html>
<?php
require_once('DbConnection.php');
if(!empty($_GET['id'])){
$id = $_GET['id'];
$sql = "DELETE FROM tbUser WHERE id=".$id;
$result = mysql_query($sql);
if(!$result){
echo "ERRO ao reomver usuário";
exit;
}else{
header("Location:list.php");
}
}
?>
\ No newline at end of file
......@@ -2,4 +2,19 @@
display: table;
margin: auto;
padding: 10px;
}
.button-success,
.button-error{
color: white;
border-radius: 4px;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
}
.button-success {
background: rgb(28, 184, 65); /* this is a green */
}
.button-error {
background: rgb(202, 60, 60); /* this is a maroon */
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment