简体   繁体   English

如何使用插入ID插入另一个表?

[英]How can I insert to another table using the insert id?

I've got a problem like this , I cannot get the id of the parent table . 我遇到这样的问题,我无法获取父表的ID。

table_name : transaction_tbl

- transaction_id
-file_name
-file_path
-description

table_name : transaction_details
-details_id
-transaction_id
- details 

here is my code to insert : 这是我要插入的代码:

$sql = "INSERT INTO transaction_tbl (`file_name`,`file_path`,`description`) VALUE('$file_name','$file_path','$description') " ;

$query = $conn->query($sql);
$transaction_id = mysqli_insert_id($conn);
if ($query === True ){
      $sql = "INSERT INTO transaction_details (`transaction_id`,`details`) VALUES ($transaction_id,$details) ";
}else {
      trigger_error('Wrong SQL: ' . $sql . 'Error: ' . $conn->error, E_USER_ERROR);
}

Now I can insert in my transaction_tbl but not in transaction_details. 现在,我可以在transaction_tbl中插入,但不能在transaction_details中插入。 what should I do? 我该怎么办? Can somebody help me? 有人可以帮我吗?

Add $query = $conn->query($sql); 添加$query = $conn->query($sql); inside if condition as below if condition如下

if ($query === True ){
      $sql = "INSERT INTO transaction_details (`transaction_id`,`details`) VALUES ($transaction_id,$details) ";
      $query = $conn->query($sql);
}
else {
          trigger_error('Wrong SQL: ' . $sql . 'Error: ' . $conn->error, E_USER_ERROR);
}

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

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