doukao8851
2012-03-14 15:51
浏览 22
已采纳

从一个表中获取最后一个插入ID以放入另一个表中

probably a simple one for you developers out there

I have this code to insert an order_id and order_name into the 'orders' table:

<?php
// start the session handler
require_once('dbfunction.php');

//connect to database
$conn = DB();

require_once('header.php');

//should we process the order?
if (isset($_POST['process'])) {

$order_name = $_POST['order_name'];

//create initial order
$stmt = $conn2->prepare("INSERT INTO orders (order_name) VALUES (?)");

//bind the parameters
    $stmt->bind_param('s', $order_name);

    // Execute query
    $stmt->execute();

I now want to insert the order items into the order_items table and I cant seem to keep that same ID that was created when inserting into the 'orders' table and add it to the 'order_items' table along with the order_items. Here is my code:

//this gets the most recent auto incremented ID from the database - this is the order_id we have just created
$order_id = mysql_insert_id();

//loop over all of our order items and add to the database
foreach ($_SESSION['order'] as $item) {

  $prod_id = $item['prod_id'];
  $quantity = $item['quantity'];
  $prod_type = $item['prod_type'];

  $stmt = $conn2->prepare("INSERT INTO order_items (order_id, prod_id, quantity, prod_type) VALUES (?, ?, ?, ?)");

  //bind the parameters
    $stmt->bind_param('iiis', $order_id, $prod_id, $quantity, $prod_type);

    // Execute query
    $stmt->execute();
}

    echo "<p class='black'>Order Processed</p>";

图片转代码服务由CSDN问答提供 功能建议

对于那些开发人员来说可能是一个简单的

我有这个代码 将order_id和order_name插入'orders'表:

 &lt;?php 
 //启动会话处理程序
require_once('dbfunction.php'); \  n 
 //连接到数据库
 $ conn = DB(); 
 
require_once('header.php'); 
 
 //我们应该处理订单吗?
if(isset($ _ POST ['  process'])){
 
 $ order_name = $ _POST ['order_name']; 
 
 //创建初始订单
 $ stmt = $ conn2-&gt; prepare(“INSERT INTO orders(order_name)VALUES  (?)“); 
 
 //绑定参数
 $ stmt-&gt; bind_param('s',$ order_name); 
 
 //执行查询
 $ stmt-&gt; execute()  ; 
   
 
 

我现在想要将订单商品插入到order_items表中,我似乎无法保留插入“订单”表时创建的相同ID 并将其与order_items一起添加到'order_items'表中。 这是我的代码:

  //这会从数据库中获取最新的自动递增ID  - 这是我们刚刚创建的order_id 
 $ order_id = mysql_insert_id();  
 
 //循环遍历所有订单商品并添加到数据库
foreach($ _SESSION ['order'] $ item){
 
 $ prod_id = $ item ['prod_id']; 
  $ quantity = $ item ['quantity']; 
 $ prod_type = $ item ['prod_type']; 
 
 $ stmt = $ conn2-&gt; prepare(“INSERT INTO order_items(order_id,prod_id,quantity,prod_type  )VALUES(?,?,?,?)“); 
 
 //绑定参数
 $ stmt-&gt; bind_param('iiis',$ order_id,$ prod_id,$ quantity,$ prod_type); \  n 
 //执行查询
 $ stmt-&gt; execute(); 
} 
 
 echo“&lt; p class ='black'&gt; Order Processed&lt; / p&gt;”; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doucuo4413 2012-03-14 15:53
    已采纳

    I would guess it's because whatever database library you are using is doing something to invalidate the mysql_insert_id (assuming it's even using the mysql functions). I'd suggest you look into the library to find out what method they suggest you use instead.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doushenyi9104 2012-03-14 15:54

    SQL Server has @@IDENTITY

    It looks like mySQL has LAST_INSERT_ID();

    My guess is you are using mySQL. If not, then please let me know the version so I can update

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题