Jump to content

How to insert into 2 tables with 1 query


AdRock

Recommended Posts

I have 2 tables in my MySQL database.  1 table to hold uer info and the other for newsletter signup.

 

When a user registers i give them the option to sign up to newsletter but how do i insert the email address into the 2 tables in 1 sql statement?

 

This is the main insert when a user registers

 

$sql = mysql_query("INSERT INTO users (first_name, last_name,  
            email_address, username, password, sex, signup_date) 
            VALUES('$first_name', '$last_name', '$email_address',  
            '$username', '$db_password', '$sex', now())"); 

 

here is the sql or the newsletter signup

 

mysql_query("INSERT INTO mailinglist SET email='" . $address . "'");

 

Is there a way I can insert the email address into both tables in 1 query

Link to comment
Share on other sites

$sql = "INSERT INTO users (first_name, last_name,  
           email_address, username, password, sex, signup_date) 
           VALUES('$first_name', '$last_name', '$email_address',  
           '$username', '$db_password', '$sex', now());"; 

$sql .= " INSERT INTO mailinglist SET email='" . $address . "'");"

$result = mysql_query($sql);

 

I think that will work. I don't usually use 2 queries at once.

 

I've just noticed your second query is wrong.

 

$sql .= " INSERT INTO mailinglist (email) VALUES ('$address');";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.