php mysqli table,PHP Mysqli使用auto_increment列向表中插入一行

Im working on a table that has 4 columns and the first one is an auto incrementing integer called id.

我在一個有4列的表上工作,第一個是一個名為id的自動遞增整數。

If im going to insert into this table using mysqli prepared statements I keep having trouble inserting a query that works. Using PHPmyAdmin It tells me to give it NULL. Ive tried this:

如果我要使用mysqli預處理語句插入到此表中,我一直無法插入有效的查詢。使用PHPmyAdmin它告訴我將它賦予NULL。我試過這個:

$query = "INSERT INTO tbl (id, col2, col3, col4) VALUES ('NULL', ?, ?, ?)";

$stmt -> bind_param("ssi", $col2, $col3, $col4)

and this

和這個

$query = "INSERT INTO tbl (id, col2, col3, col4) VALUES (NULL, ?, ?, ?)";

$stmt -> bind_param("ssi", $col2, $col3, $col4)

and only give bind_param 3 arguments (the last 3). Neither of those work. I also tried this:

並且只給bind_param 3個參數(最后3個)。這些都不奏效。我也試過這個:

$null = NULL;

$query = "INSERT INTO tbl (id, col2, col3, col4) VALUES (?, ?, ?, ?)";

$stmt -> bind_param("issi", $null, $col2, $col3, $col4);

None of these work. Is there a standardized way of inserting into this type of table?

這些都不起作用。是否有標准化的方式插入這種類型的表?

4 个解决方案

#1

13

Just skip the id field, MySQL will fill it automatically:

只需跳過id字段,MySQL就會自動填充:

$query = "INSERT INTO tbl (col2, col3, col4) VALUES (?, ?, ?)";

$stmt->bind_param("ssi", $col2, $col3, $col4)

#2

5

If the id field is the auto_increment, then just don't specify it in your insert query :

如果id字段是auto_increment,那么就不要在insert查詢中指定它:

$query = "INSERT INTO tbl (col2, col3, col4) VALUES (?, ?, ?)";

And, of course, don't try to bind any parameter to it ;-)

當然,不要試圖綁定任何參數;-)

As it's generated by MySQL, there is no need to pass that column.

由於它是由MySQL生成的,因此無需傳遞該列。

#3

4

This should work, because id is added automatically (incremented for this reason) by mysql:

這應該工作,因為mysql會自動添加id(由於這個原因遞增):

$query = "INSERT INTO tbl (col2, col3, col4) VALUES (?, ?, ?)";

In some cases, you have to insert auto_incremtnt field explicitly, if this is the case then you can use the INSERT IGNORE statement, see mysql manual for more info about it.

在某些情況下,您必須顯式插入auto_incremtnt字段,如果是這種情況,那么您可以使用INSERT IGNORE語句,請參閱mysql手冊以獲取有關它的更多信息。

#4

2

This one

這個

$query = "INSERT INTO tbl (id, col2, col3, col4) VALUES (NULL, ?, ?, ?)";

$stmt -> bind_param("ssi", $col2, $col3, $col4)

should surely work. What is the exact error you get?

應該肯定有用。你得到的確切錯誤是什么?

Now I look better, you have $query and $stmt. What do you have in between? Probably you are missing some part.

現在我看起來更好,你有$ query和$ stmt。你們之間有什么關系?可能你錯過了一些部分。

It should be

它應該是

$stmt = $mysqli->prepare("INSERT INTO tbl (id, col2, col3, col4) VALUES (NULL, ?, ?, ?)");

$stmt -> bind_param("ssi", $col2, $col3, $col4);

$stmt ->execute();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值