I get an error in "if ($ stmt- execute ())" with PDOStatement :: execute (): SQLSTATE [HY093]: Invalid parameter number

1

I am using the model view controller, I have a problem where I get a long error indicated in the title in the line of: "if ($ stmt-> execute ()) {", the User does not generate me in the DB and is what is bothering me the most, I attach the git link in case someone wants to execute it: link

I put the part of the code code that it carries out so that they can guide me:

  

This is the code of the connection to the DB

class Conexion{
  static public function conectar(){
    $link = new PDO("mysql:host=localhost;dbname=pos","root","");
    $link ->exec("set names utf8");
    return $link;
   }
}
  

This is the controller's

class ControladorUsuarios{  static public function ctrCrearUsuario(){

    if(isset($_POST["nuevoUsuario"])){

        if ( preg_match("/^[a-zA-Z0-9ñÑáéíóúÁÉÍÓÚ ]+$/", $_POST["nuevoNombre"]) &&
             preg_match("/^[a-zA-Z0-9ñÑáéíóúÁÉÍÓÚ]+$/", $_POST["nuevoUsuario"]) && 
             preg_match("/^[a-zA-Z0-9]+$/", $_POST["nuevaContraseña"])) {

            $tabla = "usuario";

            $datos = array("nombreUsuario" => $_POST["nuevoNombre"],
                            "nickUsuario" => $_POST["nuevoUsuario"],
                            "contraseñaUsuario" => $_POST["nuevaContraseña"],
                            "perfilUsuario" => $_POST["nuevoPerfil"]);

            $respuesta = ModeloUsuarios::mdlIngresarUsuario($tabla, $datos);

             if($respuesta == "ok") {

                    echo '<script>
                    swal({
                    type: "success",
                    title: "¡Usuario guardado correctamente!",
                    showConfirmButton: true,
                    confirmButtonText: "Cerrar",
                    closeOnConfirm: false
                    }).then((result)=>{
                        if(result.value){
                            window.location = "usuarios";
                        }   
                        });
                    </script>';
            }
            }else{

            echo '<script>
                swal({
                    type: "error",
                    title: "¡El Nombre o Usuario no puede ir vacio o llevar caracteres especiales!",
                    showConfirmButton: true,
                    confirmButtonText: "Cerrars",
                    closeOnConfirm: false
                }).then((result)=>{
                        if(result.value){
                            window.location = "usuarios";
                        }   
                });

            </script>';
        }
    }
}
  

And the latter is the model:

    require_once: "conexion.php"; class ModeloUsuarios{ static public function mdlIngresarUsuario($tabla, $datos){

    $stmt = Conexion::conectar() -> prepare("INSERT INTO 
        $tabla(nombreUsuario, nickUsuario, contraseñaUsuario, perfilUsuario, fotoUsuario)
        VALUES (:nombreUsuario, :nickUsuario, :contraseñaUsuario, :perfilUsuario, :fotoUsuario)");

    $stmt-> bindParam(":nombreUsuario", $datos["nombreUsuario"], PDO::PARAM_STR);
    $stmt-> bindParam(":nickUsuario", $datos["nickUsuario"], PDO::PARAM_STR);
    $stmt-> bindParam(":contraseñaUsuario", $datos["contraseñaUsuario"], PDO::PARAM_STR);
    $stmt-> bindParam(":perfilUsuario", $datos["perfilUsuario"], PDO::PARAM_STR);

    if( $stmt-> execute() ){
        return "ok";
    }else{
        return "error";
    }
    $stmt -> close();
    $stmt = null;
}
}
    
asked by Gabii Flores 21.12.2018 в 01:36
source

1 answer

0

The problem is here ...

$stmt-> bindParam(":contraseñaUsuario", $datos["contraseñaUsuario"], PDO::PARAM_STR);

Never use Ñ (no tilde or another special character) in your code ... or it will give you a lot of headaches ... the Ñ only for the end user, try to use English, instead of using the password pass or password.

Use this piece of code instead of the one you have.

$stmt = Conexion::conectar() -> prepare("INSERT INTO 
        $tabla( nombreUsuario, nickUsuario, contraseñaUsuario, perfilUsuario)
        VALUES (:nombreUsuario, :nickUsuario, :pass, :perfilUsuario)");

    $stmt-> bindParam(":nombreUsuario", $datos["nombreUsuario"], PDO::PARAM_STR);
    $stmt-> bindParam(":nickUsuario", $datos["nickUsuario"], PDO::PARAM_STR);
    $stmt-> bindParam(":pass", $datos["contraseñaUsuario"], PDO::PARAM_STR);
    $stmt-> bindParam(":perfilUsuario", $datos["perfilUsuario"], PDO::PARAM_STR);
    
answered by 21.12.2018 / 05:29
source