Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 插入2表并设置两个外键自动递增_Php_Mysql - Fatal编程技术网

Php 插入2表并设置两个外键自动递增

Php 插入2表并设置两个外键自动递增,php,mysql,Php,Mysql,我有一个包含两个表的数据库。当用户发布文章时,它将插入两个表中(一个文件中有两个查询) 我使用post\u id作为外键,两个表都是post\u id自动递增的。外键会弄乱吗?例如,如果用户A和B同时查询数据库 表1 post_id user... 1 A 2 B 表2 post_id content... 1 A 2 B 首先,不能在两个表上都使用自动增量 通常,您要做的是在表1中插入,获取刚刚插入行的ID 然后使用此ID,在表2中插入,该表

我有一个包含两个表的数据库。当用户发布文章时,它将插入两个表中(一个文件中有两个查询)

我使用
post\u id
作为
外键
,两个表都是
post\u id
自动递增的。外键会弄乱吗?例如,如果用户A和B同时查询数据库

表1

post_id user...
1       A
2       B
表2

post_id content...
1       A
2       B

首先,不能在两个表上都使用自动增量

通常,您要做的是在
表1
中插入
,获取刚刚插入行的
ID

然后使用此
ID
,在
表2中插入
,该表引用
表1

请参阅:mysqli::$insert_idat

示例:

$query = "INSERT INTO table1(user,whatever) VALUES ('A','something')";
$mysqli->query($query);

printf ("New Record has id %d.\n", $mysqli->insert_id);

$query = "INSERT INTO table2(post_id,content) VALUES ($mysqli->insert_id,'This is content')";
$mysqli->query($query);

首先,不能在两个表上都使用自动增量

通常,您要做的是在
表1
中插入
,获取刚刚插入行的
ID

然后使用此
ID
,在
表2中插入
,该表引用
表1

请参阅:mysqli::$insert_idat

示例:

$query = "INSERT INTO table1(user,whatever) VALUES ('A','something')";
$mysqli->query($query);

printf ("New Record has id %d.\n", $mysqli->insert_id);

$query = "INSERT INTO table2(post_id,content) VALUES ($mysqli->insert_id,'This is content')";
$mysqli->query($query);

您还可以使用基于以下内容的存储过程执行此操作:stackoverflow.com/a/1723325/1688441

DELIMITER //
CREATE PROCEDURE new_post_with_content(
  user_id CHAR(5), content_text CHAR(100)
BEGIN
START TRANSACTION;
   INSERT INTO table1 (user) 
     VALUES(user_id);

   INSERT INTO table2 (post_id, content) 
     VALUES(LAST_INSERT_ID(), content_text);
COMMIT;
END//

DELIMITER ;
你这样称呼它:

CALL new_engineer_with_task('A','This is the content');

您还可以使用基于以下内容的存储过程执行此操作:stackoverflow.com/a/1723325/1688441

DELIMITER //
CREATE PROCEDURE new_post_with_content(
  user_id CHAR(5), content_text CHAR(100)
BEGIN
START TRANSACTION;
   INSERT INTO table1 (user) 
     VALUES(user_id);

   INSERT INTO table2 (post_id, content) 
     VALUES(LAST_INSERT_ID(), content_text);
COMMIT;
END//

DELIMITER ;
你这样称呼它:

CALL new_engineer_with_task('A','This is the content');

为什么不使用表1作为用户表,第二个作为POST

users
user_id(autoinc)    username
1                   A
2                   B
3                   C

posts
post_id(autoinc)   user_id       posts_text
1                  2             text
2                  1             other text

为什么不使用表1作为用户表,第二个作为POST

users
user_id(autoinc)    username
1                   A
2                   B
3                   C

posts
post_id(autoinc)   user_id       posts_text
1                  2             text
2                  1             other text

表中只能有一个自动递增字段。使用联接而不是表中只能有一个自动递增字段。使用连接代替,所以我必须使用最后一个插入id和查询后准确。。。更好的是,您还可以使用此处描述的存储过程来满足您的需要:因此,我必须使用最后一个插入id和查询后的数据。。。并且在第二个表上没有autoincrement。更好的是,您还可以使用此处描述的存储过程来满足您的需要: