【问题标题】:How can I insert to another table using the insert id?如何使用插入 ID 插入另一个表?
【发布时间】:2015-06-26 03:49:35
【问题描述】:

我遇到了这样的问题,我无法获取父表的 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 中插入。我该怎么办?有人可以帮帮我吗?

【问题讨论】:

    标签: php mysqli insert


    【解决方案1】:

    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);
    }
    

    【讨论】:

    • ,,,, 非常感谢 ,,, 我已经知道了
    猜你喜欢
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 2016-06-07
    • 1970-01-01
    • 2017-08-02
    相关资源
    最近更新 更多