Change logged-in user status

1

Good morning, I'm learning PHP and I've created a login system. I would like help with the code to change the status of the logged in user.

I have a list of users who are online, and when I run this script, I want to change the status of the user who has already spent more than 50 minutes with online status, but he is changing the status of all users.

Here's my code:

$pdo = new PDO("mysql: host=localhost; dbname=pdo","root","");

$view = $pdo->prepare("SELECT agora,lastaccesso FROM users");
$view->execute();

foreach ($view as $mostra):
    $lastaccesso = $mostra['lastaccesso']; 
    $agora = $mostra['agora']; 
    $horaNova = strtotime("$lastaccesso + 5 minutes");
    $horaNovaFormatada = date("H:i", $horaNova);
    if ($horaNovaFormatada  < $agora):
        $altera = $pdo->prepare("UPDATE users SET online='" . $sim_nao . "'");
        $altera->execute();
        if($altera):
            echo "<script>window.location = 'online.php'</script>";
        else:
            echo "erro";
        endif;
endforeach;
    
asked by anonymous 12.09.2018 / 14:28

1 answer

0

See that you are giving update without specifying id , thus generating a change for all users.

Another thing, if you are doing a preparação of the query, why not use a placeholder in it?

Do not know if , Try something like:

if ($horaNovaFormatada  < $agora):
    $altera = $pdo->prepare("UPDATE users SET online= ? WHERE id = ?");
    $altera->bindValue(array($sim_nao, $iddouser));
    $altera->execute();
    if($altera):
        echo "<script>window.location = 'online.php'</script>";
    else:
        echo "erro";
    endif;

Change the $iddouser variable to the user id.

I hope you're writing the id of the logged in user somewhere.

    
12.09.2018 / 15:28