PHP mySQL - Insert new record into table with auto-increment on primary key

后端 未结 5 1217
予麋鹿
予麋鹿 2020-12-01 06:02

Wondering if there is a shorthand version to insert a new record into a table that has the primary key enabled? (i.e. not having to include the key column in the query) Lets

相关标签:
5条回答
  • 2020-12-01 06:39

    This is phpMyAdmin method.

    $query = "INSERT INTO myTable
             (mtb_i_idautoinc, mtb_s_string1, mtb_s_string2) 
             VALUES
             (NULL, 'Jagodina', '35000')";
    
    0 讨论(0)
  • 2020-12-01 06:52

    Use the DEFAULT keyword:

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

    Also, you can specify the columns, (which is better practice):

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

    Reference:

    • http://dev.mysql.com/doc/refman/5.6/en/data-type-defaults.html
    0 讨论(0)
  • 2020-12-01 06:55
    $query = "INSERT INTO myTable VALUES (NULL,'Fname', 'Lname', 'Website')";
    

    Just leaving the value of the AI primary key NULL will assign an auto incremented value.

    0 讨论(0)
  • 2020-12-01 06:58

    You can also use blank single quotes for the auto_increment column. something like this. It worked for me.

    $query = "INSERT INTO myTable VALUES ('','Fname', 'Lname', 'Website')";
    
    0 讨论(0)
  • 2020-12-01 07:00

    I prefer this syntaxis:

    $query = "INSERT INTO myTable SET fname='Fname',lname='Lname',website='Website'";
    
    0 讨论(0)
提交回复
热议问题