title: Assign Common ID to multiple row insert from MySQL array
body: <p>I have a PHP script that inserts rows into a table based on selected rows from a MySQL array. </p>
  
  <p>the code to insert the rows into the new table is:</p>
  
  <pre><code>$sql="insert into loaddetails (CaseNo,GrossMass,CaseStatus,Customer)
  select `case no`,`gross mass`,`case status`, customer from
  availablestock where `case no` = '$val'";
  </code></pre>
  
  <p>I want to assign all the inserted rows the same ID so that multiple <code>stock</code> items share the same LoadID.</p>
  
  <p>How can I modify my code to do this so all the inserted records share the same ID and the ID is unique to the load. </p>
  
  <p>I thought I could use the code below to get the max id and increment it by one</p>
  
  <pre><code>SELECT max(loadid)+1 from loaddetails
  </code></pre>
  
  <p>How can I acheive this? I realise my PHP code is not perfect but it is functional, I just need to add the functionality to allow for the rows to be inserted with a common ID to produce a result as below:</p>
  
  <p><img src="https://i.stack.imgur.com/UFYpe.jpg" alt="enter image description here"></p>
  
  <p>Thanks in advance for the assistance.</p>
  
  <p>Regards,
  Ryan Smith</p>
  
  <p>Complete code is:</p>
  
  <pre><code>&lt;?php
      mysql_connect("localhost", "user", "password")or die("cannot connect");    
      mysql_select_db("databasename")or die("cannot select DB");
      $sql="SELECT `case no`,`customer`,`gross mass`, `case status` from availablestock where transporttypename= 'localpmb'";
      $result=mysql_query($sql);
      $count=mysql_num_rows($result);
  ?&gt;
  &lt;table border=1&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;form name="form1" method="post"&gt;
                  &lt;table&gt;
                      &lt;tr&gt;
                          &lt;td&gt;#&lt;/td&gt;
                          &lt;td&gt;Case Number&lt;/td&gt;
                          &lt;td&gt;Customer&lt;/td&gt;    
                          &lt;td&gt;Weight&lt;/td&gt; 
                          &lt;td&gt;Status&lt;/td&gt; 
                      &lt;/tr&gt;
  &lt;?php
      while($rows=mysql_fetch_array($result)){
  ?&gt;
                      &lt;tr&gt;
                          &lt;td&gt;&lt;input type="checkbox" name=check[]  value="&lt;?php echo $rows['case no']; ?&gt;"&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;?php echo $rows['case no']; ?&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;?php echo $rows['customer']; ?&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;?php echo $rows['gross mass']; ?&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;?php echo $rows['case status']; ?&gt;&lt;/td&gt;
                      &lt;/tr&gt;                                   
  
  &lt;?php
      }
  ?&gt;
                      &lt;tr&gt;
                          &lt;td&gt;&lt;input name="planlocalpmb" type="submit" id="planlocalpmb" value="planlocalpmb"&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                      &lt;?php
  
  
  
                              $check=$_POST['check'];
  
                          if($_REQUEST['planlocalpmb']=='planlocalpmb'){
   {
                              $sql="insert into loaddetails (CaseNo,GrossMass,CaseStatus,Customer) select `case no`,`gross mass`,`case status`, customer from availablestock where `case no` = '$val'";
  
                              foreach($check as $key=&gt;$value)
                              {
                              $sql="insert into loaddetails (CaseNo,GrossMass,CaseStatus,Customer) select `case no`,`gross mass`,`case status`, customer from availablestock where `case no` = '$value'";
                              $final=mysql_query($sql);
                              if($final)
                              {
                              echo "&lt;meta http-equiv=\"refresh\" content=\"0;URL=php.php\"&gt;";
                              }                                            } 
                                  }
                                  }
                      // Check if delete button active, start this
  
  
  
  // if successful redirect to php.php
  
  mysql_close();
  ?&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>