PHP mySQL - insert a new record into a table with automatic increment by primary key

Want to know if there is an abridged version for inserting a new record into a table with the primary key turned on? (i.e. do not include the key column in the query) Assume that the key column is called ID, and the remaining columns are Fname, Lname and Website

$query = "INSERT INTO myTable VALUES ('Fname', 'Lname', 'Website')"; 
+46
php mysql insert auto-increment
Sep 20 2018-11-21T00:
source share
4 answers

Use the DEFAULT keyword:

 $query = "INSERT INTO myTable VALUES (DEFAULT,'Fname', 'Lname', 'Website')"; 

Alternatively, you can specify columns (which is better):

 $query = "INSERT INTO myTable (fname, lname, website) VALUES ('fname', 'lname', 'website')"; 

Link:

+92
20 Sep '11 at 21:45
source share

I prefer this syntax:

 $query = "INSERT INTO myTable SET fname='Fname',lname='Lname',website='Website'"; 
+12
Sep 20 2018-11-21T00:
source share
 $query = "INSERT INTO myTable VALUES (NULL,'Fname', 'Lname', 'Website')"; 

Just leave the primary key value AI NULL and assign an automatically incremented value.

+5
Sep 20 2018-11-21T00:
source share

This is the phpMyAdmin method.

 $query = "INSERT INTO myTable (mtb_i_idautoinc, mtb_s_string1, mtb_s_string2) VALUES (NULL, 'Jagodina', '35000')"; 
+4
Jun 04 '13 at 6:33
source share



All Articles