gpt4 book ai didi

php - PDO 如何从表中获取 auto_increment 值

转载 作者:行者123 更新时间:2023-11-29 02:52:46 25 4
gpt4 key购买 nike

我正在用 php 创建日历并从数据库中获取事件。我们还必须使用准备好的 PDO 语句插入事件。我可以成功创建新事件并将其插入到事件表中。我还必须与家庭成员建立联系并进行事件。

$stmt = $handle->prepare("INSERT INTO events(name, description, starthour, meridian, eventdate)
VALUES(:name, :description, :starthour, :meridian, :eventdate)");
$stmt->execute(array(
"name" => $name,
"description" => $description,
"starthour" => "$starthour",
"meridian" => "$meridian",
"eventdate" => "$eventdate"
));

$stmt1 = $handle->prepare("INSERT INTO familymemberevents(familymemberID,eventID)
VALUES(:familymemberID, :eventID)");
$stmt1->execute(array(
"familymemberID" => $familymemberid,
"eventID" =>
));

eventID 自动递增。无论如何我可以从数据库中获取这个号码吗?提前致谢!

最佳答案

$stmt = $handle->prepare("INSERT INTO events(name, description, starthour, meridian, eventdate)
VALUES(:name, :description, :starthour, :meridian, :eventdate)");
$stmt->execute(array(
"name" => $name,
"description" => $description,
"starthour" => "$starthour",
"meridian" => "$meridian",
"eventdate" => "$eventdate"
));

$eventID = $handle->lastInsertId();

if($eventID){
//Event Insert successful, continue with familymemberevent insert
$stmt1 = $handle->prepare("INSERT INTO familymemberevents(familymemberID,eventID)
VALUES(:familymemberID, :eventID)");
$stmt1->execute(array(
"familymemberID" => $familymemberid,
"eventID" => $eventID
));
}else{
//Event Insert failed - handle appropriately here
}

关于php - PDO 如何从表中获取 auto_increment 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33975746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com