Change login user status

Asked

Viewed 84 times

1

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

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

This is 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;
  • Voce is setting this variable $sim_nao somewhere?

  • Apparently you’re not closing the first if

  • I had forgotten the variable and the if , already put and the result is the same . it changes status of all users , but I just want it to be landed from users who are more than 1 hour with online status

  • tried this code but is returning me error .

  • $con=mysqli_connect("localhost","root",""","Pdo"); if (mysqli_connect_errno()) { echo "Failed to connect to Mysql: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT id_user, TIMEDIFF(lastaccess,now) AS diff FROM users"); $Row = mysqli_fetch_array($result); $sim_nao = ($Row["diff"] > 10) ? '0' : '1'; mysqli_query($con,"UPDATE users SET online='". $sim_nao." 'Where differs >= -00:25:18"); mysqli_free_result($result); mysqli_close($con); ?>

  • mysqli_fetch_array() expects Parameter 1 to be mysqli_result, Boolean Given in

Show 1 more comment

1 answer

0

See that you’re giving one update without specifying a id, thus generating a change in all users.

Another thing, if you’re making one preparação query, why not use a placeholder in the same?

In the event 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 variable $iddouser by user id.

I hope you’re recording id user logged in somewhere.

  • understood , I did with id and it worked , but I wanted to be able to sweep bank and check all users when I try to do so , all status change at once only .

  • Using the id of the logged-in user, the change will apply to all system users. How are you identifying the logged-in users of your system? Is recording the id somewhere?

Browser other questions tagged

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