Mysql – How to get the next auto-increment id in MySQL

auto-incrementinformation-schemainsertMySQL

How to get the next id in mysql to insert it in the table ?

INSERT INTO payments (date, item, method, payment_code)
VALUES (NOW(), '1 Month', 'paypal', CONCAT("chaminda", id))

Best Answer

Here is an approach for this.

Step 1: Insert without payment_code.

Step 2: Get mysql_insert_id from STEP 1.

Step 3: Update payment_code with where condition as mysql_insert_id.

Code Snippet in Php: The commands might have to be corrected.

1.    mysql_query("INSERT INTO payments (date, item, method, payment_code)
    VALUES (NOW(), '1 Month', 'paypal')");

2.     printf("Last inserted record has id %d\n", mysql_insert_id());

3.     mysql_query(“UPDATE payments set payment_code=CONCAT("chaminda",mysql_insert_id()) where id=mysql_insert_id());