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

  • SEARCH
  • Home
  • 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 7786491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:25:32+00:00 2026-06-01T20:25:32+00:00

I’m currently in the process of finishing a PHP script which parses emails and

  • 0

I’m currently in the process of finishing a PHP script which parses emails and inserts them into database tables structured like so –

Users table

  • userId – int – auto_increment – PK
  • email – varchar

Emails table

  • emailId – int – auto_increment – PK
  • userId – int – FK
  • attachmentId – int – FK
  • body – varchar
  • subject – varchar

Attachments table

  • attachmentId – int – auto_increment – PK
  • attachmentName – varchar
  • fileType – varchar
  • content – mediumblob
  • code – varchar

My problem is that once I have parsed the email, how do I ensure that my foreign key relationships are correct? My initial approach was just to perform:

INSERT INTO users (email) values ('$emailFrom');
INSERT INTO emails (subject, body) values ('$subject', '$body');
INSERT INTO attachments (attachmentName, fileType, content, code) values ('$attachmentName', '$fileType', '$content', '$code');

but then I realised that this won’t deal with my FK relationships in the emails table. Is the best approach to perform each query individually, re-query for the generated Id then place the new Id in the related FK row? Sorry if I’m not being clear.

Would there be any shortcomings in making this into one table and avoiding this problem?

  • 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-01T20:25:33+00:00Added an answer on June 1, 2026 at 8:25 pm

    I would recommend using a transaction, ensuring that the totality of the inserts is treated as atomic (one insert). This protects your data in the case that if one of the queries fail, your data doesn’t end up in a malformed state.

    Your code didn’t elude to the type of API you are using (mysql, mysqli, pdo), but I will give a snippet using PDO (untested):

    <?php 
    try {
    
        $dbo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password'); 
    
        $stmt1 = $dbo->prepare('INSERT INTO users SET email = :email'); 
        $stmt2 = $dbo->prepare('INSERT INTO attachments SET attachment_name = :attachmentName, file_type = :fileType');
        $stmt3 = $dbo->prepare('INSERT INTO emails SET set user_id = :userId, subject = :subject, body = :body, attachment_id = :attachmentId');
    
        try { 
    
            $dbo->beginTransaction(); 
    
            $stmt1->bindParam(':email', $email, PDO::PARAM_STR);
            $stmt->execute();
    
            $userId = $dbo->lastInsertId();
    
            $stmt2->bindParam(':attachmentName', $attachmentName, PDO::PARAM_STR);
            $stmt2->bindParam(':fileType', $fileType, PDO::PARAM_STR);
            $stmt2->execute();
    
            $attachmentId = $dbo->lastInsertId();
    
            $stmt3->bindParam(':userId', $userId, PDO::PARAM_INT);
            $stmt3->bindParam(':subject', $subject, PDO::PARAM_STR);
            $stmt3->bindParam(':body', $body, PDO::PARAM_STR);
            $stmt3->bindParam(':attachmentId', $attachmentId, PDO::PARAM_INT);
            $stmt3->execute();
    
            $dbo->commit(); 
    
        } catch(Exception $e) { 
            $dbo->rollback(); 
            error_log($e->getMessage()); 
        } 
    } catch(Exception $e) { 
        error_log($e->getMessage());
    } 
    

    ?>

    You can read more about API calls used in the snippet using these links:

    • bindParam

    • beginTransaction

    • commit

    • rollback

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words

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.