PDO视图插入查询的详细信息
PDO view insert details of query
我正在向 table 中插入一些数据,我需要获取该查询的 ID,以便在该查询之后直接插入到另一个查询中。
$database = DatabaseFactory::getFactory()->getConnection();
$addproduct = $database->prepare("INSERT INTO products(title, description, price,
startingprice, buyitnow, itemcondition, shortdescription, enddate, offers,
location, shipping, quantity, date, username) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$result = $addproduct->execute(array($title, $maindescription, $price, $startingprice,
$type, $condition, $shortdescription, $enddate, $offers, $country,
$shipping, $quantity, date("Y-m-d: H:i:s"), $user->username));
print_r($result);
if($result):
//add bid for min price to stop fetching every second
$addbid = $database->prepare("INSERT INTO bids(bids_amount,bids_timestamp, bids_username, bids_item) VALUES(?,?,?,?)");
$addbid->execute(array($price, strtotime("NOW"), $user->username, $result->id));
return 2;
else:
return 3;
endif;
如您所见,我正在尝试获取最后一个查询的 ID 并将其插入我的第二个查询 'bids_items'。
如何获取我在第一个查询中插入的数据数组?
只需使用 LastInsertId:
$result = $addproduct->execute(...);
$inserted_id = $database->lastInsertId;
然后在接下来的查询中根据需要使用 $inserted_id
我正在向 table 中插入一些数据,我需要获取该查询的 ID,以便在该查询之后直接插入到另一个查询中。
$database = DatabaseFactory::getFactory()->getConnection();
$addproduct = $database->prepare("INSERT INTO products(title, description, price,
startingprice, buyitnow, itemcondition, shortdescription, enddate, offers,
location, shipping, quantity, date, username) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$result = $addproduct->execute(array($title, $maindescription, $price, $startingprice,
$type, $condition, $shortdescription, $enddate, $offers, $country,
$shipping, $quantity, date("Y-m-d: H:i:s"), $user->username));
print_r($result);
if($result):
//add bid for min price to stop fetching every second
$addbid = $database->prepare("INSERT INTO bids(bids_amount,bids_timestamp, bids_username, bids_item) VALUES(?,?,?,?)");
$addbid->execute(array($price, strtotime("NOW"), $user->username, $result->id));
return 2;
else:
return 3;
endif;
如您所见,我正在尝试获取最后一个查询的 ID 并将其插入我的第二个查询 'bids_items'。
如何获取我在第一个查询中插入的数据数组?
只需使用 LastInsertId:
$result = $addproduct->execute(...);
$inserted_id = $database->lastInsertId;
然后在接下来的查询中根据需要使用 $inserted_id