I can not prepare a query to the DB

0

I'm new to PHP and MySQL, right now.

When I try to send the data of a user that is registering, it does not do anything to me, and when placing an else in one of the conditionals I realize that the query that is being prepared does not validate it.

PHP code:

    <?php session_start();

require_once 'core/config.inc.php';

$ok = false;

spl_autoload_register( function($clase) {
    require_once "core/$clase.php";
});

    if( $_POST ){
        extract( $_POST, EXTR_OVERWRITE );
        $name = strtolower( $name );

        $db = new Database( DB_HOST, DB_USER, DB_PASS, DB_NAME );

        if ( $name && $email && $confirmemail && $password && $confirmpassword ) {

            $expreg = '/^[A-z0-9\._-]+@[A-z0-9][A-z0-9-]*(\.[A-z0-9_-]+)*\.([A-z]{2,6})$/';
            if ( preg_match( $expreg, $email ) ) {
                if ( strlen( $password ) >= 6 ) {
                    if ( $password == $confirmpassword ) {
                        $validaremail = $db->validarDatos( 'email', 'users', $email );
                        if ( $validaremail == 0 ) {
                            if ($check) {
                                if( $db->preparar( "INSERT INTO users VALUES ( NULL, '$name', '$surname', '$email',  '$password', '$ci', '$year', '$country' )" ) ){
                                $db->ejecutar();
                                echo 'Te has registrado';
                                $ok = true;
                            } else {
                                echo 'Error al insertar en la base de datos';
                            }
                            }
                        }
                    }
                }
            }
        }
    }

HTML:

    <?php require './core/head.php' ;?>
    <!--Login Container-->
    <div class="container">
        <div class="row justify-content-center align-items-center">
            <div class="login col-md-4">
                <form action="" method="post">
                    <div clas="form-group">
                        <label for="formGroupExampleInput">Nombre</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="">
                        <label for="formGroupExampleInput">Apellido</label>
                        <input type="text" class="form-control" id="surname" name="surname" placeholder="">
                        <label for="formGroupExampleInput">Correo Electronico</label>
                        <input type="email" class="form-control" id="email" name="email" placeholder="">
                        <label for="formGroupExampleInput">Confirmar Correo</label>
                        <input type="email" class="form-control" id="confirmemail" name="confirmemail" placeholder="">
                        <label for="formGroupExampleInput">Contraseña</label>
                        <input type="password" class="form-control" id="password" name="password" placeholder="">
                        <label for="formGroupExampleInput">Confirmar Contraseña</label>
                        <input type="password" class="form-control" id="confirmpassword" name="confirmpassword" placeholder="">
                        <label for="formGroupExampleInput">Edad</label>
                        <input type="text" class="form-control" id="year" name="year" placeholder="">
                        <label for="formGroupExampleInput">Numero de identificacion ciudadana (C.I, DNI, etc)</label>
                        <input type="text" class="form-control" id="ci" name="ci" placeholder="">
                        <label for="formGroupExampleInput">Pais</label>
                        <input type="text" class="form-control" id="country" name="country" placeholder="">
                        <label class="custom-control custom-checkbox">
                        <input type="checkbox" class="custom-control-input" name="check">
                        <span class="custom-control-indicator"></span>
                        <span class="custom-control-description">Aceptar Terminos y Condiciones</span><br>
                        </label>
                        <button type="submit" class="btn btn-login col-md-12">Entrar</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <!--Login Container (END)-->

<?php require 'core/footer.php' ;?>

File where I have the class Database:

    <?php

class Database {
    public $db;
    protected $resultado;
    protected $prep;
    protected $consulta;

    public function __construct($dbhost, $dbuser, $dbpass, $dbname ){
        $this->db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

        if( $this->db->connect_errno ){
            echo 'Fallo al establecer contacto con la DB.';
        }

        $this->db->set_charset( DB_CHARSET );

    }

    public function getUser() {
        $this->resultado = $this->db->query( "SELECT * FROM users" );
        return $this->resultado->fetch_assoc();
    }

    public function getAssoc() {
        return $this->result->fetch_assoc();
    }

    public function preparar ( $consulta ){
        $this->consulta = $consulta;
        $this->prep = $this->db->prepare( $this->consulta );
        if ( !$this->prep ) {
            echo "Error al preparar la consutla";
        }
    }

    public function ejecutar() {
        $this->prep->execute();
    }

    public function prep() {
        return $this->prep;
    }

    public function resultado() {
        return $this->prep->fetch();
    }

    public function cambiarDatabase ( $db ) {
        $this->db->select_db( $db );
    }

    public function validarDatos( $columna, $tabla, $condicion ) {
        $this->resultado = $this->db->query( "SELECT $columna FROM $tabla WHERE $columna = '$condicion'" );
        $chequear = $this->resultado->num_rows;
        return $chequear;
    }
}

?>

config file:

    <?php
define( 'DB_HOST', 'localhost' );
define( 'DB_USER', 'root' );
define( 'DB_PASS', '' );
define( 'DB_NAME', 'projetclearning' );
define( 'DB_CHARSET', 'utf8' );

?>

and lastly the DB:

    
asked by Daniel Parra 12.10.2017 в 21:24
source

0 answers