查询不将空数据作为空数据插入 DB (MAMP)
Query not inserting empty data as empty into DB (MAMP)
我在其他网站上看到本地服务器的 php.ini 以不同的方式配置。但是,我相信通过手动填写其余列,应该能够插入数据。它甚至没有向我显示任何警报。这是我的代码。
$datos = array("nombre" => $_POST["nuevoNombre"],
"usuario" => $_POST["nuevoUsuario"],
"password" => $encriptar,
"rol" => $_POST["nuevoRol"]);
$respuesta = ModeloUsuarios::mdlIngresarUsuario($datos);
if ($respuesta == 'ok') {
echo '<script>
swal({
type: "success",
title: "Se ha creado un nuevo usuario",
showConfirmButton: true,
confirmButtonText: "Cerrar"
}).then(function(result){
if(result.value){
window.location = "usuarios";
}
});
</script>';
} else {
echo '<script>alert("asdf");</script>';
}
} else {
echo '<script>
swal({
type: "error",
title: "Por favor llena los campos correctamente",
showConfirmButton: true,
confirmButtonText: "Cerrar"
}).then(function(result){
if(result.value){
window.location = "usuarios";
}
});
</script>';
}
}
}
static public function mdlIngresarUsuario($datos){
$statement = Conexion::conectar()->prepare("INSERT INTO usuarios (nombre, usuario, password, rol, estado, ultimo_login)
VALUES (, :nombre, :usuario, :password, :rol, :estado, :ultimo_login)");
$statement->bindParam(":nombre", $datos["nombre"], PDO::PARAM_STR);
$statement->bindParam(":usuario", $datos["usuario"], PDO::PARAM_STR);
$statement->bindParam(":password", $datos["password"], PDO::PARAM_STR);
$statement->bindParam(":rol", $datos["rol"], PDO::PARAM_STR);
$statement->bindParam(":estado", '0', PDO::PARAM_STR);
$statement->bindParam(":ultimo_login", '0', PDO::PARAM_STR);
if ($statement->execute()) {
return "ok";
} else {
return "error";
}
}
也许可以尝试使用 ->execute()
删除 bindParam
并更改为:
$statement->execute(array(
':nombre' => $datos['nombre'],
':usuario' => $datos['usuario'],
':password' => $datos['password'],
':rol' => $datos['rol'],
':estado' => 0
));
我认为您在非字符串上使用 PDO::PARAM_STR
是它失败的原因之一。我还注意到 ultimo_login
在数据库中有一个默认值,因此从您的插入 sql 中删除并从执行数组中删除,它会自动正确插入日期时间戳。
我在其他网站上看到本地服务器的 php.ini 以不同的方式配置。但是,我相信通过手动填写其余列,应该能够插入数据。它甚至没有向我显示任何警报。这是我的代码。
$datos = array("nombre" => $_POST["nuevoNombre"],
"usuario" => $_POST["nuevoUsuario"],
"password" => $encriptar,
"rol" => $_POST["nuevoRol"]);
$respuesta = ModeloUsuarios::mdlIngresarUsuario($datos);
if ($respuesta == 'ok') {
echo '<script>
swal({
type: "success",
title: "Se ha creado un nuevo usuario",
showConfirmButton: true,
confirmButtonText: "Cerrar"
}).then(function(result){
if(result.value){
window.location = "usuarios";
}
});
</script>';
} else {
echo '<script>alert("asdf");</script>';
}
} else {
echo '<script>
swal({
type: "error",
title: "Por favor llena los campos correctamente",
showConfirmButton: true,
confirmButtonText: "Cerrar"
}).then(function(result){
if(result.value){
window.location = "usuarios";
}
});
</script>';
}
} }
static public function mdlIngresarUsuario($datos){
$statement = Conexion::conectar()->prepare("INSERT INTO usuarios (nombre, usuario, password, rol, estado, ultimo_login)
VALUES (, :nombre, :usuario, :password, :rol, :estado, :ultimo_login)");
$statement->bindParam(":nombre", $datos["nombre"], PDO::PARAM_STR);
$statement->bindParam(":usuario", $datos["usuario"], PDO::PARAM_STR);
$statement->bindParam(":password", $datos["password"], PDO::PARAM_STR);
$statement->bindParam(":rol", $datos["rol"], PDO::PARAM_STR);
$statement->bindParam(":estado", '0', PDO::PARAM_STR);
$statement->bindParam(":ultimo_login", '0', PDO::PARAM_STR);
if ($statement->execute()) {
return "ok";
} else {
return "error";
}
}
也许可以尝试使用 ->execute()
删除 bindParam
并更改为:
$statement->execute(array(
':nombre' => $datos['nombre'],
':usuario' => $datos['usuario'],
':password' => $datos['password'],
':rol' => $datos['rol'],
':estado' => 0
));
我认为您在非字符串上使用 PDO::PARAM_STR
是它失败的原因之一。我还注意到 ultimo_login
在数据库中有一个默认值,因此从您的插入 sql 中删除并从执行数组中删除,它会自动正确插入日期时间戳。