简体   繁体   中英

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. However, when I check the database, the value 0 is entered. 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. 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 .

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.

@Colin,

PDO's last insert id returns the value of an auto-increment primary key, if I'm not completely mistaken. It looks to me like $query's table doesn't have this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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