Registration using PDO, error 22527?

Asked

Viewed 67 times

0

I have this PHP code below that will be executed after the form Submit to register in the database. If you go through validation, a new user will be created and hashed and then entered into the database!

if (input::exists()) {
  if(Token::check(Input::get('token'))) {


    $validate = new validate();
    $validation = $validate->check($_POST, array(
      'nome' => array(
        'required' => true,
        'min' => 2,
        'max' => 50
      ),
      'sexo' => array(
        'required' => true
      ),
      'email' => array(
        'required' => true,
        'min' => 11,
        'max' => 40,
        'unique' => 'dados'
      ),
      'email2' => array(
        'required' => true,
        'matches' => 'email'
      ),
      'senha' => array(
        'required' => true,
        'min' => 6
      ),
      'senha1' => array(
        'required' => true,
        'matches' => 'senha'
      ),
      'cpf' => array(
        'required' => true
      ),
      'tel' => array(
        'required' => true
      ),
      'cel' => array(
        'required' => true
      ),
      'cep' => array(
        'required' => true
      ),
      'rua' => array(
        'required' => true
      ),
      'numero' => array(
        'required' => true
      ),
      'bairro' => array(
        'required' => true
      ),
      'cidade' => array(
        'required' => true
      ),
      'uf' => array(
        'required' => true
      ),
      'instituição' => array(
        'required' => true
      ),
      'nivel' => array(
        'required' => true
      ),
      'curso' => array(
        'required' => true
      ),
      'area' => array(
        'required' => true
      ),
      'turno' => array(
        'required' => true
      ),
      'palestras' => array(
        'required' => true
      ),
      'cidade2' => array(
        'required' => true
      ),
    ));

    if ($validation->passed()) {
      $user = new User();

      $salt = Hash::salt(32);

      try {
        $user->create(array(
          'nome' => input::get('nome'),
          'salt' => $salt,
          'data' => input::get('data'),
          'sexo' => input::get('sexo'),
          'email' => input::get('email'),
          'email2' => input::get('email2'),
          'senha' => Hash::make(input::get('senha'), $salt),
          'senha1' => Hash::make(input::get('senha1'), $salt),
          'cpf' => input::get('cpf'),
          'tel' => input::get('tel'),
          'cel' => input::get('cel'),
          'cep' => input::get('cep'),
          'rua' => input::get('rua'),
          'numero' => input::get('numero'),
          'bairro' => input::get('bairro'),
          'cidade' => input::get('cidade'),
          'uf' => input::get('uf'),
          'instituição' => input::get('instituição'),
          'nivel' => input::get('nivel'),
          'curso' => input::get('curso'),
          'area' => input::get('area'),
          'turno' => input::get('turno'),
          'palestras' => input::get('palestras'),
          'cidade2' => input::get('cidade2'),
          'group1' => 1
        ));
      } catch(Exception $e) {
        die($e->getMessage());
      }
    } else{
      foreach ($validation->errors() as $error) {
        echo $error, '<br>';
      }
    }
  }
}


?>

However an error is happening, because whenever I try to make the registration is executed the throw new Exception('Teve um problema ao se tentar criar uma conta!'); that is within the User class. I checked the classes, and I realized that the query is created normally, but in the third if of the code below, the query is not executed, making it an error. I asked to show the error and the number appears 22527

public function query($sql, $params = array()){
    $this->_error = false;
    if ($this->_query = $this->_pdo->prepare($sql)) {
        $x = 1;
        if (count($params)) {
            foreach ($params as $param) {
                $this->_query->bindValue($x, $param);
                $x++; 
                var_dump($this->_query);
            }
        } 


        if ($this->_query->execute()) {
            $this->_results = $this->_query->fetchALL(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
        } else{
            echo error_reporting(E_ALL);
            $this->_error = true;
        }
    }
    return $this;


}

Does anyone know what this mistake is and how to solve it? I researched it, but could not understand what it really is!

Below the User class:

public function __construct($user = null){
    $this->_db = DB::getInstance();
}

public function create($fields = array()) {
    if (!$this->_db->insert('dados', $fields)) {
        throw new Exception('Teve um problema ao se tentar criar uma conta!');
    }
}

} ?>

The lower the function Insert():

    public function insert($table, $fields = array()){
        $keys = array_keys($fields);
        $values = '';
        $x = 1;

        foreach ($fields as $field) {
            $values .= '?';
            if ($x < count($fields)) {
                $values .= "', '";
            }

            $x++;
        }

        $sql = "INSERT INTO `{$table}` (`".implode('`, `', $keys)."`) VALUES (".str_repeat ( "?, " , count($keys)-1 )."?)";

        if(!$this->query($sql, $fields)->error()) {
            return true;
        }
    return false;
}

Hash class:

<?php
class Hash {
public static function make($salt = '', $string = '') {
    return hash('sha256', $salt . $string);
}

public static function salt($length) {
    return random_bytes($length);
}

public static function unique() {
    return self::make(uniqid());
}
}
?>

  • 1

    is using Laravel?

  • 1

    @Virgilionovic No, actually I’ve never heard!

  • 1

    In the method query() in the else make a print_r($this->_query->errorInfo()); must show the complete error.

  • @rray It was a syntax problem, I a part of the code was with ç e acento. Now it worked out. But I’d like to ask you something, I always have trouble displaying errors, you know some video or article that explains about it?

  • 1

    You’re doing it all static the methods of this project?

  • @Virgilionovic Yes, I find it easier not to have to be instantiating a new object. That’s bad?

  • 2

    @Arthuroliveira is ... some things can even be made like this, but, your project as a whole not ... !!!

  • @Virgilionovic got it, I’ll look more on when to use and when not and improve the code, thank you so much for the tip!

  • 1

    About bug directive see that question. I recommend that you install Xdebug to track the flow of code with breakpoints and most importantly if you’re going to throw an exception or return an error write to a log or if you’re in a development environment display the error on the screen but don’t display something like deu erro. When a problem occurs in the code include as many details as possible.

  • Thank you @rray I will study more on the subject!!

Show 5 more comments
No answers

Browser other questions tagged

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