我遇到了这样的问题,无法获取父表的id。
table_name : transaction_tbl
- transaction_id
-file_name
-file_path
-description
table_name : transaction_details
-details_id
-transaction_id
- details
这是我要插入的代码:
$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);
}
现在我可以插入我的transaction_tbl,但不能插入transaction_details。我该怎么办?有人能帮我吗?
在if condition
内添加$query = $conn->query($sql);
,如下
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);
}