I have this PHP code below that will be executed after submitting the form to register in the database. If you pass the validation, a new user will be created and done HASH and then the insertion in 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>';
}
}
}
}
?>
But an error is happening, because every time I try to register, the throw new Exception('Teve um problema ao se tentar criar uma conta!');
that is inside the User class is executed. 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, causing it to give an error. I asked to show the error and the number 22527
appears
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 error is and how to solve it? I researched it, but I 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!');
}
}
} ? >
Down the insert () function:
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());
}
}
?>