INSERT into MYSQL 然后在关闭连接之前 select 插入 id,然后将这个 id 添加到其他 3 个表

INSERT into MYSQL and then before closing the connection select the insert id and then adding this id to 3 other tables

我对 SQL 的世界还很陌生,很抱歉我对此一无所知。

我在管理页面上有一个表单,可以将玩家添加到团队数据库中。提交表单后,我需要做的是:

以下是我尝试过的最佳表现:

if(isset($_POST['submit'])){ 

$first_name = mysqli_real_escape_string($con2, $_POST['first_name']);
$last_name = mysqli_real_escape_string($con2, $_POST['last_name']);
$email = mysqli_real_escape_string($con2, $_POST['email']);
$validation_code = md5($email + microtime());


$sql0 ="INSERT INTO players 
(first_name, last_name, email, validation_code)                        
VALUES ('$first_name', '$last_name','$email', '$validation_code')";



$sql01 = "SELECT player_id FROM players WHERE email='$email'";
$result01 = $con2->query($sql01);

if ($result01->num_rows > 0) {
$row01 = $result01->fetch_assoc();

$playerID = $row01['player_id'];
echo $playerID; //In for debugging. Sometimes it works sometimes it doesn't


$sql02 = "INSERT INTO team_players, cards (player_id, team_id)
VALUES('$playerID', '$id')";

感谢您对此的任何帮助。

您不能使用一个查询插入两个 table。

您可以使用一个事务,并将它们都包含在一个事务中。

否则为每次插入执行两个单独的查询。 还有一件事,你还没有执行插入到第一个 table

的查询
  START TRANSACTION;
INSERT INTO team_players (player_id, team_id) VALUES (...);
INSERT INTO cards (player_id, team_id) VALUES  (...);
COMMIT;