如何增加会话变量以进行提交

I have form to add employess to database. I have to count how many employess were added during one session and display it somewhere on the screen.

Right now my form_validation.php looks like this:

<?php
 session_start();

 if(formValidationIsCorrect){
   $_SESSION['counter']++;
   header("location:pageWithFormDetails.php);
 }

in pageWithFormDetails.php I have to display employee information from form (name, age, etc.), and how many employess were added during this session.

My problem is, that when on pageWithFormDetails.php I do

<?php echo $_SESSION['counter']; ?> it always displays 1, even If i refill form few times. I guess everytime I complete form, my counter variable is reset, and then incremented by 1. Is there a way not to reset this variable every time I complete form, and validate it?

You need to use database input and then output. When you are attempting to $_SESSION ++. There you can use ++ in db variable then -1 when you are destroying the session. That's how I did this when I needed similar type of thing.

You can try this code snippet and change your code accordingly

<?php
session_start();
if(isset($_POST['submit'])){
  array_key_exists('counter', $_SESSION) ? $_SESSION['counter']++ : ($_SESSION['counter'] =1);
  echo $_SESSION['counter'];
}
?>
<form action="" method="post">
    <input type="submit" name="submit" value="submit" />
</form>