No error message appears in php, but it does not work

Asked

Viewed 415 times

1

I made a registration form and an Insert query with Pdo, but when entering the data and pressing the button to call the query nothing happens, no error messages appear, nor in the log, there is something wrong in the codes ? ;-;

Query Insert

class Cadastro {

    private $con;
    private $serial;
    private $login;
    private $email;
    private $rrname;
    private $senha;
    private $nome;

    public function __construct() {
        $this->con = new Conexao();
    }

    public function queryInsert($dados) {
        try {
            $this->login = $dados['login'];
            $this->senha = sha1($dados['senha']);
            $this->email = $dados['email'];
            $this->rrname = $dados['rrname'];
            $cst = $this->con->conectar()->prepare("INSERT INTO 'contas' ('login', 'senha', 'email', 'nomenorr') VALUES (:login, :senha, :email, :rrname)");
            $cst->bindParam(':login', $this->login);
            $cst->bindParam(':senha', $this->senha);
            $cst->bindParam(':email', $this->email);
            $cst->bindParam(':rrname', $this->rrname);
            if ($cst->execute()) {
                return 'sucess';

            } else {
                return 'error';
            }
        } catch (PDOException $ex) {

        }
    }

Connection class

<?php

    class Conexao {

        private $servidor;
        private $banco;
        private $user;
        private $password;
        private static $pdo;

        public function __construct() {
            $this->servidor = "localhost";
            $this->banco = "rrcard";
            $this->user = "root";
            $this->password = "12345";
        }

        public function conectar() {
            try {
                if (is_null(self::$pdo)) {
                    self::$pdo = new PDO("mysql:host=" . $this->servidor . ";bdname" . $this->banco, $this->user, $this->password);
                }
                return self::$pdo;
            } catch (PDOException $ex) {

            }
        }
    }

php of the htm page

<?php
require_once '/src/domain/conta.php';

$Cad = new Cadastro();

if (isset($_POST['btCad'])) {
    if ($Cad->queryInsert($_POST) == 'sucess') {
       header("location: ./index.php");
    } else {
       header("location: ./cadastro.php");
    }
}
?>

Html registration form

<form method="POST" action="">
    Login:<br>
    <input type="text" name="login" class="campo" maxlength="40" required autofocus><br>
    Senha:<br>
    <input type="password" name="senha" class="campo" maxlength="40" required><br>
    Email:<br>
    <input type="email" name="email" class="campo" maxlength="50" required><br>
    Nome no RR:<br>
    <input type="text" name="rrname" class="campo" maxlength="40" required><br>
    <br><br>
    <input type="submit" name="btCad" value="Confirmar" class="btn">
    <br><br>
    <input type="checkbox" value="check" required>
    Ao criar a conta você confirma que leu e concorda com os nossos <a id="terms" class="terms" href="#"> termos de uso </a>
</form>
  • you don’t even have a button calling the action="/aquivoParaExecutarOMetodo" <button type="Submit">save</button>

  • But the button that is there is already being called in php code, so I don’t need to put a path in the action=", no ?

  • Have you ever considered displaying a message when an exception is captured? You capture the exception and do nothing with it.

2 answers

2


First present possible errors or exceptions to be displayed:

In your Queryinsert method use errorInfo() and $ex->getMessage()

public function queryInsert($dados) {

        try {
            $this->login = $dados['login'];
            $this->senha = sha1($dados['senha']);
            $this->email = $dados['email'];
            $this->rrname = $dados['rrname'];

            $cst = $this->con->conectar()->prepare("INSERT INTO 'contas' ('login', 'senha', 'email', 'nomenorr') VALUES (:login, :senha, :email, :rrname)");
            $cst->bindParam(':login', $this->login);
            $cst->bindParam(':senha', $this->senha);
            $cst->bindParam(':email', $this->email);
            $cst->bindParam(':rrname', $this->rrname);                      

            if ($cst->execute()) {               
                return 'sucess';
            } else { 
                var_dump($cst->errorInfo());            
            }
        } catch (PDOException $ex) {
            echo $ex->getMessage();
        }
    }

Another possible scenario, depending on the location and nomenclature, your code may be using the header for the Register class and thus does not present any results, even if you put to display the errors or exceptions comment the header("location: ./cadastro.php");

require_once '/src/domain/conta.php';
$Cad = new Cadastro();

if (isset($_POST['btCad'])) {

    $dados = $_POST;   

    if ($Cad->queryInsert($dados) == 'success') {
       header("location: ./index.php");
    } else {
       //header("location: ./cadastro.php");
    }
}

0

Well your Action is empty, more if you adjust and it continues without result, it is probably an error in MYSQL, one way to see this is to take the Mysql code and test in the database itself, if it works there and the application continues without anything happening, it is very likely that the field: emails, password, etc... Is passing empty values, and if your table does not accept empty "NOT NULL"', will give error in the bank!

  • You don’t need anything in the action, the action is being done in php of the page, and I tested the database and it’s working :/, the data being written in the fields is appearing in echo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.