Problem when inserting data in MySQL table with PHP

0

Hello friends I have a problem and I can not insert data in a mysql table, although I already used the same code in another project, I do not understand where the error is because it just does not throw me any error when sending the form. I've been stuck with this for several days and it sure is some silly thing that I'm not realizing ...

This is my config.php.

<?php 


// --------------- CONEXION A LA BD ---------------

$db = 'merlo_turismo';
$usuario = 'root';
$passw = '';

try { $con = new PDO("mysql:host=localhost;dbname=$db;charset=utf8", $usuario, $passw);
} catch (PDOException $e) {
    print "¡Error!: " . $e->getMessage() . "<br/>";
    die();
}

// ------------ FIN CONEXION A LA BD ---------------

 ?>

And this is the code:     

    require 'config.php';

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $user = filter_var(strtolower($_POST['reg_usuario']), FILTER_SANITIZE_STRING);
        $email = $_POST['reg_email'];
        $password = $_POST['reg_pass'];
        $password2 = $_POST ['reg_pass2'];

        $errores = '';

        if (empty($user) or empty($password) or empty($email) or empty($password2)) {
            $errores .= '<li>Por favor rellena todos los datos correctamente</li>';
        } else {

            $statement = $con->prepare('SELECT * FROM users WHERE nombre_usuario = :usuario LIMIT 1');
            $statement->execute(array(':usuario'=> $user));
            $resultado = $statement->fetch();

            if ($resultado != false) {
                # Esto significaría que ya existe el usuario
                $errores .= '<li>Ese nombre de usuario ya existe</li>';
            }

            $password = hash('sha512', $password);
            $password2 = hash('sha512', $password2);

            if ( $password != $password2) {
                # Si las contraseñas son diferentes:
                $errores .= '<li>Las contraseñas no son iguales</li>';
            }

        }

        if ($errores == '' && $_POST['submit'])  {
            # esto significaria que no hay errores

            $statement2 = $con->prepare('INSERT INTO users (nombre_usuario,email,pass) VALUES (NULL, :usuario, :email, :pass)');
            $statement2->execute(array(
                ':usuario' => $user,
                ':email' => $email,
                ':pass'=> $password
                ));

            var_dump($statement2);

            // header('Location: login.php');
        }
    }

require 'views/registro.view.php';
 ?>

and this is the form from which I send the data:

<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" class="form-contacto">
    <p>Los campos marcados con un <span class="text-warning">*</span> son necesarios</p>

    <div class="form-group">
        <label for="reg_usuario">Por favor ingresa tu nombre</label>
        <input id="reg_usuario" name="reg_usuario" type="text" placeholder="Nombre de usuario *" class="form-control" value="">
    </div>
    <div class="form-group">
        <label for="reg_email">Ingresa tu E-mail</label>
        <input id="reg_email" name="reg_email" type="email" placeholder="E-mail *" class="form-control" value="">
    </div>
    <div class="form-group" >
        <label for="reg_pass">Ingresa una contraseña</label>
        <input type="password" name="reg_pass" id="reg_pass" class="form-control">
    </div>
    <div class="form-group" >
        <label for="reg_pass2">Ingresa tu contraseña nuevamente</label>
        <input type="password" name="reg_pass2" id="reg_pass2" class="form-control">
    </div>

    <?php if (!empty($errores)): ?>  <!-- Si la variable errores no está vacía significa que si hay errores-->
        <div class="alert error">
            <?php echo $errores; ?>
        </div>
    <?php endif; ?>

    <input type="submit" name="submit" class="btn btn-primary" value="Crear una cuenta">
</form>
    
asked by Santiago Tolosa 31.05.2018 в 21:08
source

1 answer

0

Try to insert that query directly in MySQL, if there is an error, it will return it to you, from my point of view: I do not like the user much, maybe the error is there

    
answered by 31.05.2018 в 21:15