查询未将空数据作为空插入数据库(MAMP)

时间:2018-10-05 15:13:42

标签: php mysql 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";
}

}

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

也许可以尝试使用->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中删除并从execute数组中删除,它将自动正确地插入日期时间戳。