title: Php counter and Substring Concatenation
body: <p>I am working on a car rental system, where the user can add a car into the php script which thereby adds the values into the back end dababase. I have a field called 'ID' which is auto incremented INT field. Everytime I add a car, the value is automatically added as an incremented Integer. Now, how I actually want it to be is different, for example if the name of the car to be added is 'MERCEDES', the ID value should be MER001. If the next car then added is VAUXHALL, the ID value should be VAU002 and so on. I know how to get 'MER' out of mercedes using the substring function in php, but I dont know how to have a loop counter and concatenate it with the extracted substring. I believe for this the ID field in my database table will have to be a VARCHAR but I dont know if a VARCHAR can be auto-incrementing (doesnt make sense). Here's the form to add the car.
       
       <h1>Add a new Car to the database</h1>
       <p>Fill in the details below to Register</p></p>
  
  <pre><code>  &lt;label&gt; Car Name
    &lt;span class="small"&gt;Enter car name&lt;/span&gt;
    &lt;/label&gt;
    &lt;input type="text" name="CARNAME" id="CARNAME" /&gt;
  
    &lt;label&gt;Fuel Type
    &lt;span class="small"&gt;Eg: Petrol&lt;/span&gt;
    &lt;/label&gt;
    &lt;input type="text" name="FUELTYPE" id="FUELTYPE" /&gt;
  
    &lt;label&gt;Transmission
    &lt;span class="small"&gt;Eg: Manuel&lt;/span&gt;
    &lt;/label&gt;
    &lt;input type="text" name="TRANSMISSION" id="TRANSMISSION" /&gt;
  
    &lt;label&gt;Engine Size
    &lt;span class="small"&gt;Eg: 2.4&lt;/span&gt;
    &lt;/label&gt;
    &lt;input type="text" name="ENGINE_SIZE" id="ENGINE_SIZE" /&gt;
  
    &lt;label&gt;Doors
    &lt;span class="small"&gt;Eg: 4&lt;/span&gt;
    &lt;/label&gt;
    &lt;input type="text" name="DOORS" id="DOORS" /&gt;
  
    &lt;label&gt;Total
    &lt;span class="small"&gt;Eg: 40&lt;/span&gt;
    &lt;/label&gt;
    &lt;input type="text" name="TOTAL" id="TOTAL" /&gt;
  
    &lt;label&gt;Available
    &lt;span class="small"&gt;Eg: 40&lt;/span&gt;
    &lt;/label&gt;
    &lt;input type="text" name="AVAILABLE" id="AVAILABLE" /&gt;
  
  
    &lt;input type="submit" name="submit" value="Add Car"&gt;
    &lt;div class="spacer"&gt;&lt;/div&gt;
  
    &lt;/form&gt;
  </code></pre>
  
  <p>This form takes you to adding.php which has the following code:</p>
  
  <pre><code>&lt;?php
  $link = mysql_connect ("xxxxx", "xxxxx", "xxxxx");
  mysql_select_db ("xxxxx");
  
  $ID = $_POST['ID'];
  $CARNAME = $_POST['CARNAME'];
  $FUELTYPE = $_POST['FUELTYPE'];
  $TRANSMISSION = $_POST['TRANSMISSION'];
  $ENGINE_SIZE = $_POST['ENGINE_SIZE'];
  $DOORS = $_POST['DOORS'];
  $TOTAL = $_POST['TOTAL'];
  $AVAILABLE = $_POST['AVAILABLE'];
  $DATEADDED = $_POST['DATEADDED'];
  $test = substr($CARNAME,0,3); //tried to use this test variable and it works to get the substring!
  
  if($TOTAL&gt;=$AVAILABLE)
  {     
  $query = "insert into car (ID,CARNAME,FUELTYPE,TRANSMISSION,ENGINE_SIZE,DOORS,TOTAL,AVAILABLE,DATEADDED) values ('$_POST[ID]','$_POST[CARNAME]','$_POST[FUELTYPE]','$_POST[TRANSMISSION]','$_POST[ENGINE_SIZE]','$_POST[DOORS]','$_POST[TOTAL]','$_POST[AVAILABLE]',CURDATE())";
  $result = mysql_query($query);
  header("location: list_logged.php");
  }
  else
  {
  echo "&lt;script&gt;alert('The Total number of cars cannot be less than Available number!'); location.href='add.php';&lt;/script&gt;"; 
  }
  mysql_close ($link);
  
  ?&gt;
  </code></pre>
  
  <p>Any suggestions as to how I can post something to ID to make it like MER001, MAC002...and so on? Right now it is just set to an auto incrementing value, which is added automatically. I know I'd have to change the field in the database from ID to VARCHAR but I dont know what to do next. I tried searching similar questions on substr and php counter concatenation but found nothing useful.</p>