title: how to avoid overwriting of session array in php
body: <p>I have a form.In which there is a checkbox list under the text boxes. When I click on each checkbox, a modal appears with sub data according to checkbox coming from database. what I want to do is to pass data into database. That have been already done. But the problem is all the sub checkbox values do not go to database. It passes only sub categories of last selected main category into database with other post data.I think, the wrong occurs because of session array inside insert.php file. It overwrites session variable with newly coming post data.(checkbox values). I could not find to what is the wrong with it.</p>
  
  <p>I made a simple form to describe best.</p>
  
  <p><strong>config.php</strong></p>
  
  <pre><code>&lt;?php
     $servername = "localhost";
     $username = "root";
     $password = "";
     $db = "mydb";
  
   // Create connection
      $conn = mysqli_connect($servername, $username, $password,$db);
  
  // Check connection
     if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
        }
  ?&gt;  
  </code></pre>
  
  <p><strong>form.php</strong></p>
  
  <pre><code>&lt;body&gt;
  &lt;div class="form1"&gt;
  &lt;form action="passData.php" method="post"&gt;
  
      &lt;input type="text" name="name" placeholder="Name"/&gt;&lt;br&gt;
      &lt;input type="text" name="age" placeholder="age"/&gt;&lt;br&gt;
      &lt;input type="text" name="address" placeholder="address"/&gt;&lt;br&gt;
      &lt;input type="checkbox" name="favourite" value="1"/&gt;sports
      &lt;input type="checkbox" name="favourite" value="2"/&gt;fruits
      &lt;input type="checkbox" name="favourite" value="3"/&gt;colors
      &lt;div&gt;
          &lt;button class="btn btn-primary" type="submit" name="submit" id="submit"&gt;Submit&lt;/button&gt;
      &lt;/div&gt;
  &lt;/form&gt;    
  &lt;/div&gt;
  
       &lt;!--modal--&gt;
  &lt;div id="myModal" class="modal fade" role="dialog"&gt;
    &lt;div class="modal-dialog"&gt;
      &lt;div class="modal-content"&gt;
        &lt;div class="modal-header"&gt;
          &lt;button type="button" class="close" data-dismiss="modal"&gt;&amp;times;&lt;/button&gt;
          &lt;h4 class="modal-title"&gt;Modal Header&lt;/h4&gt;
        &lt;/div&gt;
        &lt;div class="modal-body"&gt;
  
        &lt;/div&gt;
        &lt;div class="modal-footer"&gt;
          &lt;button class="btn btn-primary btn-fill" id="submit" onclick="submit();" name="submit" &gt;save&lt;/button&gt;
  
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;&lt;!--modal is finished--&gt;
  
  
  &lt;/body&gt;
  </code></pre>
  
  <p><strong>Image 1</strong></p>
  
  <p><a href="https://i.stack.imgur.com/ZtOq9.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZtOq9.jpg" alt="enter image description here"></a></p>
  
  <p><strong>Image 2</strong></p>
  
  <p><a href="https://i.stack.imgur.com/7T07T.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7T07T.jpg" alt="enter image description here"></a></p>
  
  <p><strong>modal.php</strong></p>
  
  <pre><code>&lt;?php   
     if($_POST['value']){
        $value = $_POST['value'];
        include("config.php");
  
        $sql = "SELECT sub_categories FROM favourite WHERE category_id=".$value." ";
        $res = mysqli_query($conn,$sql);
        while($row = mysqli_fetch_array($res)){
        echo "&lt;input class='category' type='checkbox' name='category[]' value ='".$row['sub_categories']."'/&gt;".$row['sub_categories'];
        echo "&lt;br&gt;";
  
          }
  }
  
  
  ?&gt;
  </code></pre>
  
  <p>Here is my js file</p>
  
  <pre><code>$(document).ready(function(){
      //model 
      $("input[type=checkbox][name=favourite]").on('change',function(){
       if(this.checked) {
  
          var value = $(this).val();
          $.ajax({
              url:"modal.php",
              type:"POST",
              data:{value:value},
              success:function(modalBody){
                  $("#myModal .modal-body").html(modalBody);
                  $("#myModal").modal('show');
  
              }
  
          });
      }
         });
  });
  
  
  
   function submit() {
  
       var insert = [];
       $('.category:checked').each(function(){
             insert.push($(this).val()); 
          });
  
       insert = insert.toString();
              $.ajax({
              url:'insert.php',
              type:'POST',
              data:{insert:insert},
              success:function(){
               alert('success');
               $("#myModal").modal('hide');
        }
      }); 
  
  }
  </code></pre>
  
  <p>when I click save button on modal, selected checkbox values pass to insert.php file and store in session variable.</p>
  
  <p><strong>insert.php</strong></p>
  
  <pre><code>&lt;?php
  if (!isset($_SESSION['insert'])) {
  $_SESSION['insert'] =[];
   }
  $insert = $_POST['insert'];
  array_push($_SESSION['insert'], $insert);
  ?&gt;
  </code></pre>
  
  <p>After click on submit button all post data pass into passData.php</p>
  
  <p><strong>passData.php</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  
  include('config.php');
  
  if(isset($_POST['submit'])){
     $name=$_POST['name'];
     $age=$_POST['age'];
     $address=$_POST['address'];
  
  $sql= "INSERT INTO students(name,age,address,favourite) VALUES('$name',
         '$age',
         '$address',
         '".$_SESSION['insert']."');" ;
  $result=mysqli_query($conn,$sql);
  }
  
  
   ?&gt;
  </code></pre>
  
  <p>I try to show you my idea, hope you can understand.Help me.</p>