Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7843839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:41:10+00:00 2026-06-02T16:41:10+00:00

I have the following PHP script which runs nicely and inserts all the data

  • 0

I have the following PHP script which runs nicely and inserts all the data into MySQL using the PDO transaction correctly, but only on the first run through. After the initial run through, I try inserting the same data again. The first lastInsertId then starts to return 0 because the unique key attribute on the email field is stopping a new userIdvalue being created via auto_increment.

After reading through similar answers I’m still stuck as to how to check for this situation and fix the problem. I wrote the $userCheck so that I could see if the value from $fromParsed matched that of an email address already in the database. I don’t know how to take the value of this query and then edit the script so that if a user is already in there with the same email address then it gets the userId of $fromParsed, and if $fromParsed isn’t already in there, insert it then continue using lastInsertId(); to get the userId.

I did try searching for an answer to this but I couldn’t find anything that gave me a clear answer for a similar situation involving PDO transactions.

Sorry if the answer to this is blindly obvious.

try {

    $con = new PDO('mysql:host=localhost;dbname=db', 'root', 'password');

    $attachmentsQuery = $con->prepare('INSERT INTO attachments SET attachmentName = :attachmentName, content = :content, fileType = :fileType');
    $usersQuery = $con->prepare('INSERT INTO users SET email = :email');
    $emailsQuery = $con->prepare('INSERT INTO emails SET userId = :userId, subject = :subject, body = :body, attachmentId = :attachmentId');
    $userCheck = $con->prepare("SELECT userId FROM users WHERE email = '$fromParsed'");

    try {
        $con->beginTransaction();

        $userCheck->execute();
        //Something here?

        $usersQuery->bindParam(':email', $fromParsed, PDO::PARAM_STR);
        $usersQuery->execute();

        $userId = $con->lastInsertId();

        $attachmentsQuery->bindParam(':attachmentName', $filename, PDO::PARAM_STR);
        $attachmentsQuery->bindParam(':fileType', $fileType, PDO::PARAM_STR);
        $attachmentsQuery->bindParam(':content', $content, PDO::PARAM_LOB);
        $attachmentsQuery->execute();

        $attachmentId = $con->lastInsertId();

        $emailsQuery->bindParam(':userId', $userId, PDO::PARAM_INT);
        $emailsQuery->bindParam(':attachmentId', $attachmentId, PDO::PARAM_INT);
        $emailsQuery->bindParam(':subject', $subject, PDO::PARAM_STR);
        $emailsQuery->bindParam(':body', $text, PDO::PARAM_STR);
        $emailsQuery->execute();

        $con->commit();

        } catch(Exception $e) {
                $dbo->rollback();
                die();
        }
} catch(Exception $e) {
        error_log($e->getMessage());
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:41:12+00:00Added an answer on June 2, 2026 at 4:41 pm

    Give this a try,

    if($userCheck->execute() && $userCheck->rowCount() > 0){
        // we have a user.
        $data = $userCheck->fetch(PDO::FETCH_ASSOC);
        $userId = $data['userId'];
    }
    else {
    
        $usersQuery->bindParam(':email', $fromParsed, PDO::PARAM_STR);
        $usersQuery->execute();
    
        $userId = $con->lastInsertId();
    }
    //Something here?
    

    Brief explanation, this will place the results from the $userQuery into $data if there is a record dictated by the rowCount. If the query returns no results, it will continue to execute your $userQuery and return a userId for the newly created entry.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script which runs a MySQL query. $query = INSERT INTO
I have a (PHP) script which runs on a new row in MySQL. However,
I have a scheduled task on my server which runs a PHP script every
I have a php script which is doing the following job. $unTarCommand = 'tar
I have a script in kohana which runs fine on php 5.2.11. I took
I have the following PHP script which reads from two CSV files, At the
I have a php script which i would need the following 3 web browsers
I have the following script which runs fine but I think a) it is
I have the following php script which works flawlessly in normal circumstances (i.e. visiting
I have a php script which uses the following line to submit a form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.