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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7492145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:47:54  来源:igfitidea点击:

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

phpmysqlinsertauto-increment

提问by JimmyJammed

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

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

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

回答by regality

Use the DEFAULT keyword:

使用 DEFAULT 关键字:

$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:

参考:

回答by jenkin90

I prefer this syntaxis:

我更喜欢这种语法:

$query = "INSERT INTO myTable SET fname='Fname',lname='Lname',website='Website'";

回答by Korvin Szanto

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

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

只留下 AI 主键的值NULL将分配一个自动递增的值。

回答by minimal

This is phpMyAdmin method.

这是 phpMyAdmin 方法。

$query = "INSERT INTO myTable
         (mtb_i_idautoinc, mtb_s_string1, mtb_s_string2) 
         VALUES
         (NULL, 'Jagodina', '35000')";

回答by Rahul Bharati

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

您还可以对 auto_increment 列使用空白单引号。像这样的东西。它对我有用。

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