<?php

require_once('../../php/definitions.php');


$mysqli = new mysqli(PACKAGE_LOG_DB_SERVER, PACKAGE_LOG_DB_USER, PACKAGE_LOG_DB_PASS, PACKAGE_LOG_DB_NAME);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!$mysqli->query("DROP PROCEDURE IF EXISTS Login_IncrementAgentEmailAttempts") ||
    !$mysqli->query("
	
	CREATE PROCEDURE Login_IncrementAgentEmailAttempts(IN emaillogin VARCHAR(50))
	BEGIN

	DECLARE emailattempts INT;

	SELECT `loginattempts` FROM `agent` WHERE (`email` = emaillogin) LIMIT 1 INTO emailattempts;
	SET emailattempts = emailattempts + 1;

	UPDATE `agent`
	SET `loginattempts`=emailattempts
	WHERE (`email` = emaillogin);

	SELECT emailattempts AS emailattempts;
	END;
	
	")) {
    echo "Stored procedure creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}else{
	echo "Stored Procedure Login_IncrementAgentEmailAttempts created successfully.";
}

?>