Loop->If-Problem->Repeating all the time

My problem is the printing line: Now the card, please!
It should be written only once and after that, the program should wait for the new card.
This code should be saved and compared to the new code from the second go through.
If both codes are the same the key is the new one.

The other question is if I have to use this

if ( ! mfrc522.PICC_IsNewCardPresent())
      {
        return;
      }

      if ( ! mfrc522.PICC_ReadCardSerial())
      {
        return;
      }
      long code = 0;
      for (byte i = 0; i < mfrc522.uid.size; i++)
      {
        code = ((code + mfrc522.uid.uidByte[i]) * 10);
      }

again in the second comparising.
I did not get it if I have to use these lines every time I want to communicate with the cards.

That's the programm:

#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
long nCode;
long snCode;
int i = 1;
boolean secondTime = false;
MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup()
{
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  Serial.println("It starts now");
}

void loop()
{
  if (i == 1) //only one gothrough
  {
    boolean firstTime = true;
    if (firstTime == true)
    {
      Serial.println("Now, the new card!");
      if ( ! mfrc522.PICC_IsNewCardPresent())
      {
        return;
      }

      if ( ! mfrc522.PICC_ReadCardSerial())
      {
        return;
      }
      long code = 0;
      for (byte i = 0; i < mfrc522.uid.size; i++)
      {
        code = ((code + mfrc522.uid.uidByte[i]) * 10);
      }
      nCode = code;
      secondTime = true;
      if(firstTime == true)
      {
        Serial.println("Again please");
      }
      firstTime = false;
      
      snCode = code;
      Serial.println(snCode);
      if (snCode == nCode)
      {
        Serial.println("It works!");
        i++;
      }
      else
      {
        Serial.println("Try again");
        firstTime = true;
        secondTime = false;
      }
    }
  }
}

Thanks

Your loop checks for 'firstTime' but won't do anything if 'secondTime' is true. So you need an 'else' statement to deal with the second pass of the card.
Also, in your code you have

nCode = code;
....
snCode = code;
....
if (snCode == nCode)
....

so nCode and snCode will always be the same and the 'if' will always be true.

I hope that's enough guidance for you to restructure your logic.

In this kind of step procedures, I advice to use a variable to store the state. To see the problem as the creation of a state machine is much easier to understand, than having several flags that somehow end up building many states. This is because with a single vatable for the state, there is no place for grey areas where some undesired characteristics of other state stay for other state. The classic problem where you end up trying a million times a simple two-step process.

This is an untested, example code that explains the idea

//if needed only:
/*note that some states are transitional, and represent when arduino has to do something only once and then go to the next step */
String stateNames []={"initialization","step 1", "loading","step 2"}
byte currentState=0;

loop(){
  switch (currentState)
    case 0:
      initializePeripherals();
      state++;
      break();
    case 1:
      Serial.print("please do the first action");
      if(cardNumber)
        state++
      break;
    case 2:
      String username=database_getUserName(cardNumber);
      state++;
      break;
    case 3:
      Serial.print("ready");
      state++;
      break;
}