title: how to auto increment an input name in html and save in database?
body: <p>I have a table named <strong><em>positions</em></strong>. This table has the list of the different positions that the admin has added in the <em>listOfPositions.php</em> like President, Vice-President, etc. After adding different positions, he can now add the different people under that position. And that's where my problem is. How will I have an auto increment input name for the names of the people depending on how many positions it has in the table <strong><em>positions</em></strong> ? </p>
  
  <p>I tried using javascript, but it increments only in the html and not reflecting to the php when I try to save. My current code where the adding of the names of different persons depending on the position is the ff:</p>
  
  <pre><code>&lt;script type="text/javascript" src="http://code.jquery.com/jquery-git.js"&gt;&lt;/script&gt;
  &lt;form action="post_officers.php" method="post"&gt;&lt;br&gt;
  &lt;center&gt;&lt;select name="year"&gt;
  
  &lt;?php
  
      for($i=date('Y'); $i&gt;1999; $i=$i-2) {
      $selected = '';
      $year2 = $i-2;
          if ($year == $i) $selected = ' selected="selected"';
          echo ('&lt;option value="'.$year2. "-" . $i .'" '.$selected.'&gt; '.$year2.'-'.$i.'&lt;/option&gt;'."\n");
  }
  ?&gt;
  &lt;/select&gt;&lt;/center&gt;
  
  
      &lt;?php 
      include_once('dbcontroller.php');   
      $sql = "SELECT * FROM positions ORDER BY pos_id ASC";
      $result = mysqli_query($conn, $sql);
      /* assign an onchange event handler */
      while ($row = mysqli_fetch_array($result)) {
      $position = $row['position'];
      ?&gt;
          &lt;br&gt;&lt;br&gt;
          &lt;table id="options-table"&gt;                  
              &lt;tr&gt;
                  &lt;td&gt;&lt;input type="file" name="file" /&gt;&lt;/td&gt;    
                  &lt;td&gt;&lt;input type="hidden" name="position" /&gt;&lt;?php echo $position; ?&gt;&lt;/td&gt;
                   &lt;td&gt;&lt;input type="text" name="name" /&gt;&lt;/td&gt;
              &lt;/tr&gt;                  
  
          &lt;/table&gt;
      &lt;?php 
      }
      ?&gt;
  
      &lt;input type="submit" name="submit" value="SAVE"/&gt;
  &lt;/form&gt;         
  &lt;script&gt;            
                  $("input[name='file']").each(function(ind) {
                          $(this).attr(ind + 1);
                  });
  
                  $("input[name='position']").each(function(ind) {
                          $(this).attr(ind + 1);
                  });
  
                  $("input[name='name']").each(function(ind) {
                          $(this).attr(ind + 1);
                       });
  
  
  &lt;/script&gt;       
  </code></pre>
  
  <p>And this is my php code:</p>
  
  <pre><code>&lt;?php
     include ('dbcontroller.php');
     date_default_timezone_set('Asia/Manila');
              $year = mysqli_real_escape_string($conn,$_POST['year']);
  
     if(isset($_POST['submit'])) {
          $result = mysqli_query($conn,"SELECT * FROM officers WHERE year = '$year'");
          $num_rows = mysqli_num_rows($result);
  
          if($num_rows&gt;0){
              echo "&lt;script type='text/javascript'&gt;alert('Year already exists.'); window.location.href='create_alumni_officers.php';&lt;/script&gt;";
          }
          else {
              for ($i = 1; $i &lt;= 8; $i++) {
                          $name = mysqli_real_escape_string($conn,$_POST['name'.$i]);
                          $position = mysqli_real_escape_string($conn,$_POST['position'.$i]);
  
                          $file=(rand(1000,100000)."-".$_FILES['file'.$i]['name']);
                          $type=($_FILES['file'.$i]['type']);
                          $size=$_FILES['file'.$i]['size'];
                          $loc=($_FILES['file'.$i]['tmp_name']);
  
                          $new_size=$size/1024; // file size in KB
  
                          // make file name in lower case
                           $new_file_name = strtolower($file);
                           // make file name in lower case
  
                           $final_file=str_replace(' ','-',$new_file_name);
  
                    if(move_uploaded_file($loc, '../officers-avatars/'.$final_file)) {
                                            echo "Page is loading, please wait...";
                        $result = mysqli_query($conn,"INSERT INTO officers VALUES (id, '$year', '$position', '$name', '$final_file', '$new_size', '$type')")
                        or die(mysqli_error($conn));
                        echo ("&lt;script type='text/javascript'&gt;window.location.href='alumni_officers.php';&lt;/script&gt;");
                      }
              }
  
          }
     }
  
  
  ?&gt;
  </code></pre>
  
  <p>And this doesn't work at all. Any help? I hope you guys understood what I'm trying to ask.</p>