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;
}
}