dongshao5573 2016-04-13 15:30
浏览 262

mysqli - 可以在事务中获取前两个查询的insert_id吗?

Is it possible to get the insert_id of the previous two queries? I am able to get the last one, but would like the previous two. In the below example, once the form in processed, I would like to add a new role to the address table, new row to the role table then add a row to the customers using the id's from the queries inserted in the previous two queries. Is this at all possible.

if(isset($_POST["submit"])) :
  $username = $_POST["username"];
  $password = $_POST["password"];
  $role = $_POST["role"];
  $permission1 = intval($_POST["permission1"]);
  $permission2 = intval($_POST["permission2"]);
  $city = $_POST["city"];
  $state = $_POST["state"];
  mysqli_autocommit($connection,FALSE);
  mysqli_query($connection,"INSERT INTO Address(city, state) VALUES('{$city}', '{$state}')");
  mysqli_query($connection,"INSERT INTO Roles(roleName, permission1, permission2) VALUES('{$role}', '{$permission1}', '{$permission2}')");
  mysqli_query($connection,"INSERT INTO Customers(username, password, roleId, addressId) VALUES ('{$username}', '{$password}'," .  mysqli_insert_id($connection) . " , " . mysqli_insert_id($connection) . ")");
  if(mysqli_error($connection)):
    echo mysqli_error($connection);
  endif;
  mysqli_commit($connection);
endif;

The mysqli_insert_id($connection) in the last query will pull the Id from the Roles insert twice. Any way to get it to grab the two Id vals from the first two queries?

  • 写回答

1条回答 默认 最新

  • duanbin4847 2016-04-13 15:37
    关注
    if(isset($_POST["submit"])) :
    $username = $_POST["username"];
    $password = $_POST["password"];
    $role = $_POST["role"];
    $permission1 = intval($_POST["permission1"]);
    $permission2 = intval($_POST["permission2"]);
    $city = $_POST["city"];
    $state = $_POST["state"];
    mysqli_autocommit($connection,FALSE);
    
    mysqli_query($connection,"INSERT INTO Address(city, state) VALUES('{$city}', '{$state}')");
    //this will give you first id//
    $first_inserted_id = mysqli_insert_id($connection);
    
    mysqli_query($connection,"INSERT INTO Roles(roleName, permission1, permission2) VALUES('{$role}', '{$permission1}', '{$permission2}')");
    //this will give you second id//
    $second_inserted_id = mysqli_insert_id($connection);
    
    mysqli_query($connection,"INSERT INTO Customers(username, password, roleId, addressId) VALUES ('{$username}', '{$password}'," .    $first_inserted_id  . " , " . $second_inserted_id . ")");
    
    //this will give you third id//
    $third_inserted_id = mysqli_insert_id($connection);
    
    if(mysqli_error($connection)):
        echo mysqli_error($connection);
    endif;
        mysqli_commit($connection);
    endif;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化