0

how can i take last used index from a specific table and add a +1 to it?

database login, table cars

I think i need to add it here ####

$sql = 'INSERT INTO '.$this->options['db_table'] .' (carID,name, size, type, title, description)' .' VALUES (#############,?, ?, ?, ?, ?)';

  • Do you use MySQL? – levitologista Apr 3 '14 at 5:52
  • What did you try? Did you read up on AUTO_INCREMENT ? – Colin 't Hart Apr 3 '14 at 8:21
  • Yeah i did. But i'm new to programming so i prefer to ask + google and get more accurate information, then to just read and imagine something ;) I'm using xampp so it's a mysqli. – user36593 Apr 3 '14 at 9:02
1

The best way for that is to use the sequence way which is auto increment facility, example:

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
) ENGINE=MyISAM;

so in that case you don't need to mention that id in the insert like:

INSERT INTO animals (name) VALUES
    ('dog'),('cat'),('penguin'),
    ('lax'),('whale'),('ostrich');

also you can get the inserted id from the function

 LAST_INSERT_ID()

so here the id is auto last rec id + 1, you can see full reference about it from MySQL dev site: http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy