I'm trying to insert a user's registry, but POST
does not occur. It always falls into the echo '<script type="text/JavaScript">alert("Erro ao cadastrar!")</script>';
exception. Below is my code, I ask that if someone identifies where I went wrong, help me.
cadastro.php
<?php
require_once 'php/classes/funcoes.class.php';
require_once 'php/classes/usuarios.class.php';
$objFc = new Funcoes();
$objUs = new Usuarios();
if (isset($_POST['btnCadastrar'])) {
if ($objUs->qryInsert($_POST) == 'ok') {
header('location: cadastro.php');
}else{
echo '<script type="text/JavaScript">alert("Erro ao cadastrar!")</script>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- Tell the browser to be responsive to screen width -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<!-- Favicon icon -->
<link rel="icon" type="image/png" sizes="16x16" href="images/favicon.png">
<title>Sistema</title>
<!-- Bootstrap Core CSS -->
<link href="css/lib/bootstrap/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/helper.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:** -->
<!--[if lt IE 9]>
<script src="https:**oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script><scriptsrc="https:**oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body class="fix-header fix-sidebar">
<!-- Preloader - style you can find in spinners.css -->
<div class="preloader">
<svg class="circular" viewBox="25 25 50 50">
<circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="2" stroke-miterlimit="10" /> </svg>
</div>
<!-- Main wrapper -->
<div id="main-wrapper">
<div class="unix-login">
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-lg-4">
<div class="login-content card">
<div class="login-form" id="formulario">
<h4>Cadastro</h4>
<form name="formCadastro" action="" method="POST">
<div class="form-group">
<input type="text" class="form-control" placeholder="Nome" required="required" name="nome" value="">
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Email" required="required" name="email" value="" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$">
</div>
<div class="form-group">
<input type="password" class="form-control" placeholder="Senha" required="required" name="senha" value="">
</div>
<!--<div class="checkbox">
<label>
<input type="checkbox" required="required" name="termos" value=""> Aceito os termos e condições de uso
</label>
</div>REVISAR-->
<input type="submit" class="btn btn-primary btn-flat m-b-30 m-t-30" name="btnCadastrar" value="Cadastrar">
<div class="register-link m-t-15 text-center">
<p>Possui uma conta? <a href="..\sistema\index.php" target="_blank"> Acessar</a></p>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- End Wrapper -->
<!-- All Jquery -->
<script src="js/lib/jquery/jquery.min.js"></script>
<!-- Bootstrap tether Core JavaScript -->
<script src="js/lib/bootstrap/js/popper.min.js"></script>
<script src="js/lib/bootstrap/js/bootstrap.min.js"></script>
<!-- slimscrollbar scrollbar JavaScript -->
<script src="js/jquery.slimscroll.js"></script>
<!--Menu sidebar -->
<script src="js/sidebarmenu.js"></script>
<!--stickey kit -->
<script src="js/lib/sticky-kit-master/dist/sticky-kit.min.js"></script>
<!--Custom JavaScript -->
<script src="js/custom.min.js"></script>
</body>
</html>
users.class.php
<?php
require_once 'conexao.class.php';
require_once 'funcoes.class.php';
class Usuarios {
private $con;
private $objfunc;
private $user_id;
private $user_nome;
private $user_email;
private $user_senha;
private $user_data_cadastro;
public function __construct(){
$this->con = new Conexao();
$this->objfunc = new Funcoes();
}
public function __set($atributo, $valor){
$this->$atributo = $valor;
}
public function __get($atributo){
return $this->$atributo;
}
public function qrySeleciona($dado){
try {
$this->user_id = $this->objfunc->base64($dado, 2);
$cast = $this->con->conectar()->prepare("SELECT 'user_id', 'user_nome', 'user_senha', 'user_email' FROM 'usuarios' WHERE user_id = :idUser;");
$cast->bindParam(":idUser", $this->id_user, PDO::PARAM_INT);
$cast->execute();
return $cast->fetch();
} catch (PDOException $ex) {
return 'Erro ' . $ex->getMessage();
}
}
public function qrySelect(){
try {
$cast = $this->con->conectar()->prepare("SELECT 'user_id', 'user_nome', 'user_senha', 'user_email' FROM 'usuarios';");
$cast->execute();
return $cast->fetch();
} catch (PDOException $ex) {
return 'Erro '. $ex->getMessage();
}
}
public function qryInsert($dados){
try {
$this->user_nome = $this->objfunc->tratarCaracter($dados['nome'], 1);
$this->user_email = $dados['email'];
$this->user_senha = sha1($dados['senha']);
$this->user_data_cadastro = $this->objfunc->dataAtual(2);
$cst = $this->con->conectar()->prepare("INSERT INTO 'usuarios'('user_nome', 'user_senha', 'user_email', 'user_data_cadastro') VALUES (:nome, :senha, :email, :data);");
$cst->bindParam(":nome", $this->user_nome, PDO::PARAM_STR);
$cst->bindParam(":email", $this->user_email, PDO::PARAM_STR);
$cst->bindParam(":senha", $this->user_senha, PDO::PARAM_STR);
$cst->bindParam(":data", $this->user_data_cadastro, PDO::PARAM_STR);
if ($cst->execute()) {
return 'ok';
}else{
return 'erro';
}
} catch (PDOException $ex) {
return 'Erro ' . $ex->getMessage();
}
}
public function qryUpdate($dados){
try {
$this->user_id = $this->objfunc->base64($dados['id'], 2);
$this->nome = $this->objfunc->tratarCaracter($dados['nome'], 1);
$this->email = $dados['email'];
$cst = $this->con->conectar()->prepare("UPDATE 'usuarios' SET 'user_nome' = :nome, 'user_email' = :email WHERE 'user_id' = :id;");
$cst->bindParam(":id", $this->user_id, PDO::PARAM_INT);
$cst->bindParam(":nome", $this->user_nome, PDO::PARAM_STR);
$cst->bindParam(":email", $this->user_email, PDO::PARAM_STR);
if ($cst->execute()) {
return 'ok';
}else{
return 'erro';
}
} catch (PDOException $ex) {
return 'Erro ' . $ex->getMessage();
}
}
public function qryDelete($dado){
try {
$this->user_id = $this->objfunc->base64($dados['id'], 2);
$cst = $this->con->conectar()->prepare("DELETE FROM 'USUARIOS' WHERE 'user_id' = :id;");
$cst->bindParam(":id", $this->user_id, PDO::PARAM_INT);
if ($cst->execute()) {
return 'ok';
}else{
return 'erro';
}
} catch (PDOException $ex) {
return 'Erro ' . $ex->getMessage();
}
}
}
?>
funcoes.class.php
<?php
class Funcoes{
public function tratarCaracter($valor, $tipo){
switch ($tipo) {
case 1:
$rst = utf8_decode($valor);
break;
case 2:
$rst = htmlentities($valor, ent_quotes,"ISO-8859-1");
break;
return $rst;
}
}
function dataAtual($tipo){
switch ($tipo) {
case 1:
$rst = date("Y-m-d");
break;
case 2:
$rst = date("Y-m-d H:i:s");
break;
case 3:
$rst = date("d/m/Y");
break;
}
return $rst;
}
public function base64($vlr, $tipo){
switch ($tipo) {
case 1:
$rst = base64_encode($valor);
break;
case 2:
$rst = base64_decode($valor);
break;
return $rst;
}
}
}
?>
connection.class.php
<?php
class Conexao {
//atributo privado
private $usuario;
private $senha;
private $banco;
private $servidor;
private static $pdo;
//construtor
public function __construct(){
$this->servidor = "localhost";
$this->banco = "sistema";
$this->usuario = "root";
$this->senha = "";
}
//conexao
public function conectar(){
try {
if (is_null(self::$pdo)) {
self::$pdo = new PDO("mysql:host=".$this->servidor.";dbname=".$this->banco, $this->usuario, $this->senha);
}
return self::$pdo;
} catch (PDOException $ex) {
echo $ex->getMessage();
}
}
}
?>