I'm doing a simple registration program, but I do not know why Form data is not being saved in the Database (MYSQL).
Can someone please give me some help on this?
Follow the Gadget:
<?php
include("conexao.php");
If(isset($_POST['nome'])){
$nome = filter_input(INPUT_POST, "nome", FILTER_SANITIZE_STRING);
$cpf = filter_input(INPUT_POST, "cpf", FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, "email", FILTER_SANITIZE_STRING);
$senha = filter_input(INPUT_POST, "senha", FILTER_SANITIZE_STRING);
$nascimento = DateTime::createFromFormat('d/m/Y',(filter_input(INPUT_POST, "nascimento", FILTER_SANITIZE_STRING)));
$cadastro = DateTime::createFromFormat('d/m/Y',(filter_input(INPUT_POST, "cadastro", FILTER_SANITIZE_STRING)));
$setor = filter_input(INPUT_POST, "setor", FILTER_SANITIZE_STRING);
$cargo = filter_input(INPUT_POST, "cargo", FILTER_SANITIZE_STRING);
$privilegio = filter_input(INPUT_POST, "privilegio", FILTER_SANITIZE_STRING);
If(!$conn){
die("Falha na conexão com o banco de dados, procure o administrador do sistema e informe o erro abaixo: ". "<br>" .mysqli_connect_error());
}
If(empty($nome)||empty($cpf)){
echo "Preencha os campos corretamente!";
exit;
} else {
$sqlusuarios = "INSERT INTO TB_USUARIOS (nome, cpf, email, senha, nascimento, cadastro, setor, cadastro, privilegio) VALUES ('".$nome."', '".$cpf."', '".$email."', '".$senha."', '".$nascimento->format('Y-m-d')."', '".$cadastro->format('Y-m-d')."', '".$setor."', '".$cargo."', '".$privilegio."')";
} If(mysqli_query($conn, $sqlusuarios)){
echo "<script>Alert('Usuario cadastrado com Sucesso!');</script>";
} else {
"Erro: " .$sqlusuarios. "<br>" .mysqli_connect_error($conn);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>DR3 WEB</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript">
function mask_date(field){
if(document.getElementById(field).value.length == 2){
document.getElementById(field).value = document.getElementById(field).value + "/";
}
if(document.getElementById(field).value.length == 5){
document.getElementById(field).value = document.getElementById(field).value + '/';
}
}
</script>
</head>
<body>
<br><br>
<div class="container">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">DR3 WEB</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="index.php">Logout</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Cadastros
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="empresas.php">Empresas</a></li>
<li><a href="clientes.php">Clientes</a></li>
<li><a href="fornecedores.php">Fornecedores</a></li>
<li><a href="estados.php">Estados</a></li>
<li><a href="municipios.php">Municipios</a></li>
<li><a href="usuarios.php">Usuarios</a></li>
<li><a href="classificacao.php">Classificação Financeira</a></li>
</ul>
</li>
<li><a href="#">Page 2</a></li>
<li><a href="#">Page 3</a></li>
</ul>
</div>
</nav>
<br>
<h2>Cadastro de Usuarios</h2><br><hr><br>
<form class="form-horizontal" method="post" action="usuarios.php">
<div class="form-group">
<label class="control-label col-sm-1" for="email">Nome:</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="nome" name="nome">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-1" for="senha">CPF:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="cpf" name="cpf">
</div>
<label class="control-label col-sm-1" for="senha">E-mail:</label>
<div class="col-sm-4">
<input type="email" class="form-control" id="email" name="email">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-1" for="senha">Senha:</label>
<div class="col-sm-3">
<input type="password" class="form-control" id="senha" name="senha">
</div>
<label class="control-label col-sm-1" for="senha">Nascimento:</label>
<div class="col-sm-2">
<input type="datetime" class="form-control" id="nascimento" name="nascimento" onkeyup="mask_date(this.id);">
</div>
<label class="control-label col-sm-1" for="senha">Cadastro:</label>
<div class="col-sm-2">
<input type="datetime" class="form-control" id="cadastro" name="cadastro" onkeyup="mask_date(this.id);">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-1" for="senha">Setor:</label>
<div class="col-sm-2">
<input type="text" class="form-control" id="setor" name="setor">
</div>
<label class="control-label col-sm-1" for="senha">Cargo:</label>
<div class="col-sm-2">
<input type="text" class="form-control" id="cargo" name="cargo">
</div>
<label class="control-label col-sm-1" for="senha">Privilégio:</label>
<div class="col-sm-3">
<select type="text" class="form-control" id="tipo" name="privilegio">
<option value="1">Administrador</option>
<option value="2">Gerência</option>
<option value="3">Analista</option>
<option value="4">Assistente</option>
</select>
</div>
</div>
<br>
<button type="submit" class="btn btn-default" text-align=center>Cadastrar</button>
</form>
</div>
</body>
</html>