finalizado o crud básico

parent 8b6d9c2d
......@@ -5,5 +5,7 @@
mysql_connect("$hostname", "$username", "$password") or die(mysql_error());
mysql_select_db("dbCrud") or die(mysql_error());
?>
\ No newline at end of file
......@@ -3,54 +3,94 @@
require_once('DbConnection.php');
if (empty($_POST['id'])) {
salvarUsuario(formToArray());
} else {
atualizarUsuario(formToArray());
if (isset($_POST['acao'])) {
if ($_POST['acao'] == 'cadastrar') {
salvarUsuario(formToArray());
} else if ($_POST['acao'] == 'atualizar') {
atualizarUsuario(formToArray());
} else if ($_POST['acao'] == 'logar') {
logar(formToArray());
}
} else if (isset($_GET['deletar'])) {
deletarUsuario($_GET['deletar']);
}
function salvarUsuario($user) {
$values = implode(',', $user);
unset($user['id']);
$values = "'" . implode("','", $user) . "'";
$sql = "INSERT INTO tbUser (nome,login,email,senha) values ({$values}) ";
echo $sql;
$result = mysql_query($sql);
if (!$result) {
echo 'ERRO ao se cadastrar';
exit;
}else{
} else {
header("Location:list.php");
exit;
}
}
function atualizarUsuario($user) {
$values = implode(',', $user);
echo "pre" + print_r($values) + "</pre>";
$sql = "UPDATE tbUser SET nome=".$user['nome'].",login=".$user['login'].","
. "email=".$user['email']." WHERE id=".$user['id'];
//echo "pre" + print_r($values) + "</pre>";
$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{
} else {
header("Location:list.php");
exit;
}
}
function logar($user) {
$sql = "SELECT senha FROM tbUser WHERE login='{$user['login']}'";
$query = mysql_query($sql);
$result = mysql_fetch_assoc($query);
if (count($result) > 0 && $result['senha'] == $user['senha']) {
header('location: list.php');
} else {
echo 'Login ou senha incorretos';
}
}
function deletarUsuario($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");
}
}
function formToArray() {
$user = array();
$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']: "";
$user['id'] = isset($_POST['id']) ? intval($_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;
}
......
......@@ -4,8 +4,10 @@ $id = "";
$name = null;
$login = null;
$email = null;
$acao = 'cadastrar';
if (!empty($_GET['id'])) {
$id = $_GET['id'];
$acao = 'atualizar';
$sql = "Select * FROM tbUser where id=" . $id;
$result = mysql_query($sql);
......@@ -43,6 +45,7 @@ if (!empty($_GET['id'])) {
<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="<?php echo!empty($id) ? $id : ""; ?>"/>
<input type="hidden" name="acao" value="<?php echo $acao ?>"/>
</fieldset>
</form>
</div>
......
-- phpMyAdmin SQL Dump
-- version 4.0.4
-- http://www.phpmyadmin.net
--
-- Máquina: localhost
-- Data de Criação: 18-Jun-2014 às 09:14
-- Versão do servidor: 5.6.12-log
-- versão do PHP: 5.4.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Base de Dados: `dbcrud`
--
CREATE DATABASE IF NOT EXISTS `dbcrud` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `dbcrud`;
-- --------------------------------------------------------
--
-- Estrutura da tabela `tbuser`
--
CREATE TABLE IF NOT EXISTS `tbuser` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`login` varchar(255) NOT NULL,
`senha` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
--
-- Extraindo dados da tabela `tbuser`
--
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
<!--
-->
<!DOCTYPE html>
<html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
......@@ -11,10 +11,11 @@
</head>
<body>
<div class="center-div">
<form method="post" class="pure-form">
<form method="post" action="UserController.php" class="pure-form">
<input type="text" name="login" placeholder="Login"/>
<input type="password" name="senha" placeholder="Senha" />
<input type="hidden" name="acao" value="logar"/>
<input type="submit" class="pure-button pure-button-primary" name="enviar" value="Logar"/>
</form>
......
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<?php
require('DbConnection.php');
$sql = "SELECT * FROM tbUser";
if (isset($_GET['busca'])) {
$busca = $_GET['busca'];
$where = "nome LIKE '%{$busca}%' OR email LIKE '%{$busca}%' OR login LIKE '%{$busca}%'";
$sql.= " WHERE {$where}";
}
$query = mysql_query($sql);
?>
<!DOCTYPE html>
<html>
<head>
......@@ -14,8 +24,8 @@ and open the template in the editor.
<div class="center-div">
<form method="get" class="pure-form">
<input type="text" name="busca" placeholder="Pesquisar">
<input type="submit" class="pure-button pure-button-primary" name="busca" value="Pesquisar">
<input type="reset" class="pure-button" name="busca" value="Limpar">
<input type="submit" class="pure-button pure-button-primary" value="Pesquisar">
<input type="button" onclick="window.location = 'list.php';"class="pure-button" value="Limpar">
</form>
</div>
<div class="center-div">
......@@ -32,22 +42,19 @@ and open the template in the editor.
</thead>
<?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>';
}
?>
while ($user = mysql_fetch_assoc($query)) :
?>
<tr>
<td> <?php echo $user['nome']; ?></td>
<td><?php echo $user['email']; ?></td>
<td><?php echo $user['login']; ?></td>
<td>
<a href="cadastro.php?id=<?php echo $user['id']; ?>" class="pure-button button-success">Atualizar</a>
<a href="UserController.php?deletar=<?php echo $user['id'];?>" class="pure-button button-error">Remover</a>
</td>
</tr>
<?php endwhile; ?>
</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
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