My code to increment attendance does not increment

This is all for my classwork webpage, because of the virus here in China, we can’t have presence classes. I don’t know when the govt. will change that.

I have a PDO secure login system working. I can:

  1. register a new user.
  2. login with email and password.
  3. change the password if the email is known.

These all work fine.

One more thing I need is to increment the column attendance by 1 at login time. The point of has_been_inc is to stop multiple logons from incrementing “attendance” if “has_been_inc” = 1

The table 19BE1login has the columns:

id, email, name, number, password, attendance, has_been_inc, logon_time

Using my old, insecure logon, I did this to increment attendance and has_been_incremented:

    // my stuff THIS WORKS!!
     	
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/studentdbReadfrom.inc.php' ;
    	try
      {
    	 // attendance will not increase with multiple logins. Before next week, reset has_been_incremented to zero  	
      	 $sql = 'UPDATE 19BEattendance SET attendance = attendance + 1, 
      	 has_been_incremented = has_been_incremented + 1, time = LOCALTIME() 
      	 WHERE number = ' . $_POST['password'] . ' AND has_been_incremented != 1 ;';
        $pdo->exec($sql);
          }
    	catch (PDOException $e)
      {
        $error = 'Error searching for name.';
        include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
        exit();
      }
        
        // end my stuff	THIS WORKS

Now I’m trying to translate this into a secure login format. So far I can’t get it to work. I want to put this right after the login part, so if the login is successful, the attendance will be incremented:

If the email and password are correct, I have all the details in $user:

//check if email / name in this case exists
			if($stmt->rowCount() > 0){
				//get the row
				$user = $stmt->fetch();

This is correct as shown by print_r($user):

Array ( [id] => 1 [email] => [email protected] [name] => Peter [number] => 1234567890 [password] => $2y$10$OpFPxr/WFNrXDquWimSTy.nDNhE0QsV.rVANh2gTk2g7lSF34FAb. [attendance] => [has_been_inc] => [logon_time] => 2020-08-16 10:54:32 )

“attendance” is INT(2) and “has_been_inc” is INT(1)

I get no error in apache2 error.log, my webpage opens correctly.

Below is what I tried, but I nothing happens, attendance does not go up.

Can you please help me to bend this to rights? Or suggest a better way to do this? Thanks!

	// MY STUFF HOPE THIS WORKS!!    
				try{
			 	// attendance will not increase with multiple logins. Before next week, reset 'has_been_inc' to zero from phpmyadmin
			 	$email = $user['email'];
			 	$attendance = $user['attendance']; 
			 	$has_been_inc = $user['has_been_inc'];
			 	$logon_time = $user['logon_time'];
			 	$mystmt = $pdo->prepare('UPDATE 19BE1login SET attendance = :attendance + 1, has_been_inc = :has_been_inc + 1, logon_time = LOCALTIME() WHERE email = :email AND has_been_inc != 1');	
  	 			$mystmt->execute(['attendance' => $attendance, 'has_been_inc' => $has_been_inc, 'logon_time' => $logon_time, 'email' => $email]);
      		}
				catch (PDOException $e)  				{
    				$_SESSION['exception'] = $e->getMessage();
    				//exit();
 		 		}    
   			 // END MY STUFF

I managed to make it work. This stuff is confusing for me! I think I was making it too complicated!

$mystmt = $pdo->prepare('UPDATE 19BE1login SET attendance = attendance + 1, has_been_inc = has_been_inc + 1, logon_time = LOCALTIME() WHERE email = :email AND has_been_inc != 1');	
$mystmt->execute(['email' => $email]);
Sponsor our Newsletter | Privacy Policy | Terms of Service