Search code examples
phpsqlauto-increment

How to set the data can do the increment?


May I know how to set the $n variable once I insert the data, $n will plus 1 to previous my insert number?

For similar example what I've tried:

$n = "AB000";
for ($n = 0; $n <= 0; $n++) 
{
   $query = "INSERT INTO [tablename] (user, country, batch_number) VALUES ('$user', '$country', $n)";
   // execute query
}

I want the expect result can like below the table, every time I do the insert function, the batch number can auto add 1:

user | country| batch_number
John   USA        AB009
Lawn   Germany    AB010
Shawn  England    AB011

Hope someone can guide me how to solve this problem. Thanks.


Solution

  • What you think about this script.

    This is a different approch to acheive

    online fiddle: http://sandbox.onlinephpfunctions.com/code/569b6458b3080b4fa5c5f3d96bd0839334b02e8b

    <?php
    $n = "A00";
    $user = "userName";
    $country = "country";
    for ($i=0; $i < 10; $i++) { 
        $batch = "";
      $batch = $n."".$i;
      echo "INSERT INTO tablename (user, country, batch_number) VALUE "."('".$user."', '".$country."', $batch)";
      echo "\n";
    
    }
    
    ?>