Search code examples
mysqlprimary-keyauto-increment

MySQL ID Increment Conundrum


I have a mysql table with auto increment. I'm using an id as the primary key.

Here is the structure of the key:: id int(255) No None AUTO_INCREMENT

This is how the increments have happened:

for some reason SO wasn't accepting this question with this data. Admins please note

As you can see xxxxxxxxYY series, only the YY numbers are incremented.


This is the insert statement.

$id = $this->get_next_id($this->get_last_id());
$q = 'INSERT INTO '.URL_TABLE.' (id, url, date, title, image, description) 
VALUES ("'.$id.'", "'.$url.'", NOW(), "'.$title.'", "'.$image.'", "'.$description.'")';

Solution

  • There is no need to give a new ID. Your column is AUTO_INCREMENT, so it will increment automatically (hence the name).

    Just insert like this, and let the database handle the increments:

    $q = 'INSERT INTO '.URL_TABLE.' (url, date, title, image, description) 
    VALUES ("'.$url.'", NOW(), "'.$title.'", "'.$image.'", "'.$description.'")';
    

    (Just omit the id column altogether)