Error Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in.. (path)

Iam getting values from HTML inputs and using ajax to send data from javascript to php and validate them with mysql but i get this error:

<br />n<b>Fatal error</b>:  Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\Program Files\xampp\htdocs\pruebaLogin\ajax\procesar_registro.php:12nStack trace:n#0 C:\Program Files\xampp\htdocs\pruebaLogin\ajax\procesar_registro.php(12): PDOStatement-&gt;execute()n#1 {main}n  thrown in <b>C:\Program Files\xampp\htdocs\pruebaLogin\ajax\procesar_registro.php</b> on line <b>12</b><br />n"

I notice on status is: 200 so i think the problem is maybe inside php file. I’m not sure, I am new with php.

Javascript:

$(document).on("submit", ".form_registro", function(event){
    event.preventDefault();
    var $form = $(this);
   
    var data_form = {
        nickname: $("#nickname",$form).val(),
        password: $("#password", $form).val() 
    }
    if(data_form.nickname.length < 4 ){
        $("#msg_error").text("Tu usuario no puede ser menor a 4 letras").show();
        return false;        
    }else if(data_form.password.length < 5){
        $("#msg_error").text("Tu password debe ser minimo de 8 caracteres.").show();
        return false;   
    }
    $("#msg_error").hide();
    var url_php = 'http://localhost:8077/pruebalogin/ajax/procesar_registro.php';

    $.ajax({
        type:'POST',
        url: url_php,
        data: data_form,
        dataType: 'json',
        async: true,
    })
    .done(function ajaxDone(res){
       console.log(res); 
        if(res.error !== undefined){
            $("#msg_error").text(res.error).show();
            return false;
       } 

       if(res.redirect !== undefined){
        window.location = res.redirect;
    } 
    })
    .fail(function ajaxError(e){
        console.log(e);
    })
    .always(function ajaxSiempre(){
        console.log('Final de la llamada ajax.');
    })
    return false;
});

I have this php file to check if user exists and create a new one if not

<?php
require_once "../inc/config.php";

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    header("Content-Type: application/json");
    $return_array=[];
    $nickname = strtolower($_POST['nickname']);

     /* Checking if user exists */
    $find_user = $con->prepare("SELECT * FROM usuarios WHERE nickname = '$nickname' LIMIT 1");
    $find_user->bindParam(':nickname', $nickname, PDO::PARAM_STR);
    $find_user->execute();

        /* if exists */
    if($find_user->rowCount() == 1){
        $return_array['error'] = "Este usuario ya está registrado";
        $return_array['is_login']= false;
    }else{
        $password =password_hash($_POST['password'],PASSWORD_DEFAULT);
        
        $new_user = $con->prepare("INSERT INTO usuarios (nickname, password) VALUES(:nickname, :password)");
        $new_user->bindParam(':nickname', $nickname, PDO::PARAM_STR);
        $new_user->bindParam(':password', $password, PDO::PARAM_STR);
        $new_user->execute();

        $user_id = $con->lastInsertId();
        $_SESSION['user_id']= (int) $user_id;
        $return_array['redirect']= ''; 
        $return_array['is_login']= true;
    }

    echo json_encode($return_array);

}else{
    exit("Refused");
}


?>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This line

 $find_user = $con->prepare("SELECT * FROM usuarios WHERE nickname = '$nickname' LIMIT 1");
    $find_user->bindParam(':nickname', $nickname, PDO::PARAM_STR);

Should read

 $find_user = $con->prepare("SELECT * FROM usuarios WHERE nickname = :nickname LIMIT 1");
    $find_user->bindParam(':nickname', $nickname, PDO::PARAM_STR);

When you’re using bindParam, you don’t put the data into the prepare statement you put the place holder, which in this case is :nickname


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x