简体   繁体   English

pdo lastInsertId()继续返回0

[英]pdo lastInsertId() keeps returning 0

I have two queries that insert data to their respective tables. 我有两个查询,分别将数据插入到各自的表中。 That works fine. 很好 What I have been trying to do is get the lastInsertId after each query is executed and insert those values into a third table. 我一直在尝试的是在执行每个查询后获取lastInsertId并将这些值插入到第三张表中。 However, when I check the database, the value 0 is entered. 但是,当我检查数据库时,将输入值0。 Both tables have an auto-incremented field. 两个表都有一个自动递增的字段。 Can you tell by my code why that is happening or have any suggestions? 您能通过我的代码告诉为什么发生这种情况或有什么建议吗? I'm relatively new to php so if you notice the way I'm coding is untidy, particularly at the end where I execute the queries, please tell me. 我对php比较陌生,所以如果您注意到我的编码方式不整洁,尤其是在执行查询的末尾,请告诉我。 I'd appreciate it. 我会很感激的。

if ($oneWay)
    {
        $query = "INSERT INTO journey
        (from_destination,to_destination,journey_type,depart_date,depart_time,seats_available,journey_message,user_type)
        VALUES('$pjFrom','$pjTo','$radioJourneyType', STR_TO_DATE('$departDate','%d/%m/%Y'),'$newDepTime','$seatcounter','$textareanotes','$radUserType')";
        $userID = "SELECT user_id FROM `user` ORDER BY journey_id DESC LIMIT 1";
    }
    else
    {
        $query = "INSERT INTO journey
        (from_destination,to_destination,journey_type,depart_date,depart_time,return_date,return_time,seats_available,journey_message,user_type)
        VALUES('$pjFrom','$pjTo','$radioJourneyType', STR_TO_DATE('$departDate','%d/%m/%Y'),'$newDepTime',STR_TO_DATE('$returnDate','%d/%m/%Y'),'$newRetTime ','$seatcounter','$textareanotes','$radUserType')";
        //$userID = "SELECT user_id FROM `user` ORDER BY journey_id DESC LIMIT 1";
    }
    $queryfb = "INSERT INTO user
        (facebook_id,facebook_username,facebook_first_name,facebook_last_name,facebook_image,facebook_link)
        VALUES('$hdnFacebookId','$hdnUsername','$hdnFirstName','$hdnLastName','$hdnFacebookImg','$hdnFacebookUrl')";
        //$journeyID = "SELECT journey_id FROM `journey` ORDER BY journey_id DESC LIMIT 1";

    $queryUserJourney = "INSERT INTO user_journey
                        (user_id,journey_id)
                        VALUES('$lastUserID','$lastJourneyID')";

    $db->exec($query);
    $lastUserID = $db->lastInsertId();
    $db->exec($queryfb);
    $lastJourneyID = $db->lastInsertId();
    $db->exec($queryUserJourney);//problem: 0 values being entered???
}

Updated 更新

$db->exec($query);
    $lastUserID = $db->lastInsertId();
    $db->exec($queryfb);
    $lastJourneyID = $db->lastInsertId();
    $queryUserJourney = "INSERT INTO user_journey
                        (user_id,journey_id)
                        VALUES('$lastUserID','$lastJourneyID')";
    $db->exec($queryUserJourney);working thanks to jmadsen

You might want to try 您可能要尝试

$db->lastInsertId();

... instead. ...代替。 Note the lowercase d in lastInsertId . 注意lastInsertId的小写字母d

Reference doc 参考文件

Now that I've had my coffee - you are creating the last insert statement BEFORE you populate the variables. 既然我已经喝了咖啡-在填充变量之前,您正在创建最后一个插入语句。 I think this is what Maerlyn was hinting at 我认为这是梅林暗示的

You need to move $queryUserJourney down below your 2 inserts. 您需要将$ queryUserJourney向下移动到2个插入下方。

@Colin, @Colin,

PDO's last insert id returns the value of an auto-increment primary key, if I'm not completely mistaken. 如果我没有完全弄错的话,PDO的最后一个插入ID将返回自动递增主键的值。 It looks to me like $query's table doesn't have this 在我看来$ query的表没有这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM