Problem doing password update in DB. - PHP, PDO, MYSQL-

Asked

Viewed 318 times

0

I am calling a function from a class. This function is used to change the user password in db.

The class file contains:

    <?php
class Usuario
{

    private $pdo;  /*criando variavel para usar nas funçoes*/
    public $msgErro = "";

    public function conectar($nome, $host, $usuario, $senha)
    {
        global $pdo;
        global $msgErro;
        try {
            $pdo = new PDO("mysql:dbname=" . $nome . ";host=" . $host, $usuario, $senha);
        } catch (PDOException $e) {
            $msgErro - $e->getMessage(); /*pega a mensagem de erro do php e joga na variavel msegErro e mostra pro usuario.*/
        }
    }
    public function cadastrar($nome, $email, $senha)
    {
        global $pdo;
        //global $msgErro;
        //verificando se existe usuario cadastrado.
        $sql = $pdo->prepare("SELECT id_usuario FROM usuarios WHERE email=:e"); //pega o id do usuario buscando pelo emial preenchido no cadastro
        $sql->bindValue(":e", $email);  //substitui o :e pelo email preenchido no cadastro
        $sql->execute();
        if ($sql->rowCount() > 0) //verificando houve resposta na consulta
            {
                return false; // ja tem cadastro
            } else {
            //caso nao tenha
            $sql = $pdo->prepare("INSERT INTO usuarios (nome, email, senha) VALUES (:n,:e,:s)");
            $sql->bindValue(":n", $nome);
            $sql->bindValue(":e", $email);
            $sql->bindValue(":s", md5($senha));
            $sql->execute();
            return true;
        }
    }
    public function logar($email, $senha)
    {
        global $pdo;
        //global $msgErro;
        /*verificar se o email e senha estao cadastrados, se sim*/
        $sql = $pdo->prepare("SELECT id_usuario FROM usuarios WHERE email= :e AND senha =:s");
        $sql->bindValue(":e", $email);
        $sql->bindValue(":s", md5($senha));
        $sql->execute();
        if ($sql->rowCount() > 0) //verificando houve resposta na consulta
            {
                //entrar no sistema criando uma (sessao)
                $dado = $sql->fetch(); //transforma o retorno da query em array com os nomes das colunas
                session_start();       //iniciando a sessao

                if ($_SESSION['id_usuario'] = $dado['id_usuario']) {

                    return true;
                }
            }
    }




    public function trocarSenha($novasenhaup, $id_usuario)
    {

        global $pdo;

        $sql = $pdo->prepare("UPDATE usuarios SET senha=:novasenhaup WHERE id_usuario=:id_usuario");
        $sql->bindValue(":novasenhaup", $novasenhaup);
        $sql->bindValue(":id_usuario", $id_usuario);
        $sql->execute();
        return $sql;





    }
}


?>

The specific function is to change:

    public function trocarSenha($novasenhaup, $id_usuario)
    {

        global $pdo;

        $sql = $pdo->prepare("UPDATE usuarios SET senha=:novasenhaup WHERE id_usuario=:id_usuario");
        $sql->bindValue(":novasenhaup", $novasenhaup);
        $sql->bindValue(":id_usuario", $id_usuario);
        $sql->execute();
        return $sql;

    }

In the file change some.php I have the following in the php part.

But I can’t update the password.I have the error: Notice: Undefined variable: id_usuario in C: laragon www

<body>
<div id="corpo-form-cad">
    <h1></h1>
    <form method="POST">

        <input type="password" name="novasenha" placeholder="Senha" maxlength="20">
        <input type="password" name="confSenha" placeholder="Confirmar senha">
        <input type="submit" value="Trocar senha" class="entrar">

    </form>
</div>

In the same part php file:

if (isset($_POST['novasenha'])) {

$novasenhaup = htmlentities(addslashes($_POST['novasenha']));
$confirmarSenha = htmlentities(addslashes($_POST['confSenha']));

if (!empty($novasenhaup) && !empty($confirmarSenha)) {

    $u->conectar("sistem_login", "localhost", "root", "");
    if ($u->msgErro == "") {


        if ($novasenhaup == $confirmarSenha) {
            if ($update = $u->trocarSenha($novasenhaup, $id_usuario)) {
                echo '<br>';
                echo "Cadastro realizado com sucesso!";
            }
        } else {
            echo '<br>';
            echo "Senhas não conferem!";
        }
    } else {
        echo "Erro: " . $u->msgErro;
    }
} else {
    echo "Preencha todos os campos!";
}

I can not update the password in any way, I believe it should be just a detail on some line. Remembering that in the php part I call the class as follows:

require_once 'classes/users.php';

$u = new User;

Sincerely yours Thank you!

1 answer

0


I was able to solve with only two lines. Since the error was that the id_usuario variable was undefined, I did it as follows. In the file that makes the recadastro:

session_start();
$id_usuario = $_SESSION['id_usuario'];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.