title: Mysqli Insert Inside While Loop and For loop
body: <p>I am stuck in this code, the problem is that I am trying to insert values inside the while loop and For loop, I have 584 Records and i need to insert 15 records into another table</p>
  
  <pre><code>    &lt;?PHP
  $payroll_date = '2020-01-15';
  $date_from = '2019-12-21';
  $date_to = '2020-01-05';
  
      $query = "SELECT * FROM adb_crosschex GROUP BY userid"; 
      $result = mysqli_query($db, $query); 
      if ($result) 
      { 
          $row = mysqli_num_rows($result);          
  /* ######################################################################### */
      $x = 0;
      $sqltoDTR = "SELECT * FROM adb_crosschex GROUP BY userid";
      $dtrResult = mysqli_query($db, $sqltoDTR);    
      while($rows = mysqli_fetch_array($dtrResult))  
      {
          $x++;       
          $begin = new DateTime($date_from);
          $end   = new DateTime($date_to);
  
          $pay_period = date("m-d", strtotime($date_from))." - ".date("m-d", strtotime($date_to));
  
          $idcode = $rows['idcode'];
          $controlno = $rows['userid'];
          $acctname = $rows['acctname'];
          $transdate = $rows['transdate'];
          $branch = $rows['branch'];
  
          for($i = $begin; $i &lt;= $end; $i-&gt;modify('+1 day')) {
              $date_trans = $i-&gt;format("Y-m-d");                  
  
              /* result 
                  2019-12-21
                  2019-12-22
                  2019-12-23
                  2019-12-24
                  ....... until reaching the $end         
              */
              /* mysql Insert goes here */            
          }
  
      }
  /* ######################################################################### */
          mysqli_free_result($result); 
      }   
      mysqli_close($db); 
  
  ?&gt;
  </code></pre>
  
  <p>the problem with the code is that it inserted data like forever i tried to leave it for 30 minutes and i get like 1 Million plus into my table hahahaha I supposed to get 584 x 15 = 8760 records Please Help </p>