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
https://forums.phpfreaks.com/topic/66579-how-to-insert-into-2-tables-with-1-query/
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');";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.