PHP mySQL - 将新记录插入表中,主键自动递增

2022-01-09 00:00:00 insert php mysql auto-increment

想知道是否有一个速记版本可以将新记录插入到启用了主键的表中?(即不必在查询中包含键列)假设键列称为 ID,其他列是 Fname、Lname 和网站

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 say the key column is called ID, and the other columns are Fname, Lname, and Website

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

推荐答案

使用 DEFAULT 关键字:

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')";

参考:

  • http://dev.mysql.com/doc/refman/5.6/en/data-type-defaults.html

相关文章