繁体   English   中英

最后一个mysql_insert_id与其他查询

[英]Last Id mysql_insert_id with an other query

$query = mysql_query(
  "insert into users (id,email,password,firstname,lastname) 
  values ('','$email','$password','$firstname','$lastname')"
) or die(mysql_error());

echo $userid = mysql_insert_id();

$q_query = mysql_query(
  "insert into qualification (q_id) values ('$userid')"
) or die(mysql_error());

echo $userid完美显示,但是该行未插入到qualification表中。 为什么不?

我怀疑@Gil有正确的答案。 但是,使用mysql函数last_insert_id()是更简单的方法:

mysql> create temporary table users (id int unsigned not null auto_increment primary key, email text, password text, firstname text, lastname text);
Query OK, 0 rows affected (0.00 sec)

mysql> insert into users (id,email,password,firstname,lastname) values ('','dan@site.com', 'password','dan','f');
ERROR 1366 (HY000): Incorrect integer value: '' for column 'id' at row 1

这取决于您是否处于严格模式(感谢下面的评论)

mysql> insert into users (id,email,password,firstname,lastname) values (null,'dan@site.com', 'password','dan','f');
Query OK, 1 row affected (0.00 sec)

mysql> create table qualification (q_id int unsigned not null);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into qualification( q_id) values (last_insert_id() );
Query OK, 1 row affected (0.00 sec)

mysql> select * from users,qualification;
+----+--------------+----------+-----------+----------+------+
| id | email        | password | firstname | lastname | q_id |
+----+--------------+----------+-----------+----------+------+
|  1 | dan@site.com | password | dan       | f        |    1 |
+----+--------------+----------+-----------+----------+------+
1 row in set (0.00 sec)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM