0

Where is the error of the structure in this code, because I have seen that in other code it works well?

Fatal error: Uncaught Error: Call to a member function setNombre() on bool in C:\xampp\htdocs\brightside\controlador\usuario.controller.php:95 Stack trace: #0 C:\xampp\htdocs\brightside\index.php(15): usuarioController->actualizar() #1 {main} thrown in C:\xampp\htdocs\brightside\controlador\usuario.controller.php on line 95

controller/user.controller.php:

public function actualizar() {
    $id = $_GET["idu"]??"";

    if (!empty($id)):
        $usin = Usuario::getUsuarioById($_GET["idu"]);
        if (isset($_GET["nom"])):
            $usin->setNombre($_GET["nom"]);
            $usin->setUsuario($_GET["usu"]);
            $usin->setEmail($_GET["ema"]);
            $usin->setPassword($_GET["pass"]);

            $usin->actualizar();
            $this->index();
        else:
            $nombre = $usin->getNombre();
            $usuario = $usin->getUsuario();
            $email = $usin->getEmail();
            $password = $usin->getPassword();
            require_once "vista/actualizar.usuario.php";
        endif;
        else:
        echo "mal";
    endif;
}

model/user.php:

public function actualizar() {
    $db = Database::getInstancia();
    $db->query("UPDATE usuario SET nombre=:nom, usuario=:usu, email=:ema, password=:pass WHERE usuario=:usu;",
        [
        ":nom"=>$this->nombre,
        ":usu"=>$this->usuario,
        ":ema"=>$this->email,
        ":pass"=>$this->password
        ]);
}
karel
  • 5,489
  • 46
  • 45
  • 50
Sara
  • 1
  • 2
    `$usin` is a boolean while you're using it as an object. Check what you get back from `Usuario::getUsuarioById($_GET["idu"]);` instead of assuming it returns what you want. – M. Eriksson Mar 07 '19 at 17:41
  • `$usin` is false, so it failed to find that user. Double-check the value of `$_GET["idu"]` and make sure that user ID exists. – aynber Mar 07 '19 at 17:41
  • 2
    **Warning:** You seem to be storing the passwords in clear text. Only store password hashes. Use PHP's [`password_hash()`](http://php.net/manual/en/function.password-hash.php) and [`password_verify()`](http://php.net/manual/en/function.password-verify.php) . If you're running a PHP version lower than 5.5 (which I _really_ hope you aren't), you can use the [password_compat library](https://github.com/ircmaxell/password_compat) to get the same functionallity. – M. Eriksson Mar 07 '19 at 17:43
  • I would also recommend that you use POST instead of GET when you send data to the back end for updates etc. POST is for sending data. GET is for getting data. – M. Eriksson Mar 07 '19 at 17:45
  • Ideally you should check if `$usin` is a object with `is_object()` or a faster method `(object)$usin === $usin` after you use `Usuario::getUsuarioById(..)` And return `NULL` in the `Usuario::getUsuarioById(..)` when no record(s) where found. – Raymond Nijland Mar 07 '19 at 17:50
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – miken32 Mar 07 '19 at 18:03
  • @RaymondNijland - I would expect that `is_object()` is faster than first casting the result as an object and then do a strict type comparison. (I don't have any proof though) – M. Eriksson Mar 07 '19 at 18:04
  • @MagnusEriksson In general PHP is faster in type casting than in calling functions, but it all depends on the use case. In this scenario I doubt it would make much difference and the function call is more readable. – Dharman Mar 07 '19 at 19:29
  • for `is_int($value)` or `(int)$value === $value` for example there is a difference @MagnusEriksson for objects the size of the object might also matter which is faster also PHP engine seams to matter.. the object cast in PHP 5.6.29 was faster then calling `is_object()` and in PHP 7 `is_object()` was the faster method. – Raymond Nijland Mar 07 '19 at 19:37

0 Answers0