inserting values in to multiple tables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

inserting values in to multiple tables

Post by hame22 »

Hi I have a php script which i need to run a mysql query to insert values into two tables of my database.

My first question is can this be done in one query statement?

and my second question is

I have 2 tables 1 to hold member subscription details and another to hold member details

i want to update both tables at once however the subscriptions table needs the members id from the members table which is automaically generated by the databse.
So my second question is how can I get this value when updating the member table so that I can simultaneously update the subscriptions table??

I hope this makes sense

thanks in advance
bigdavemmu
Forum Newbie
Posts: 22
Joined: Wed Oct 26, 2005 4:01 am

Post by bigdavemmu »

I think you need two seperate queries for the INSERTS. If you need the last inserted auto incremented ID use the following :

$variable_holding_ID = mysql_INSERT_ID();

This is for MySQL BTW.

When you come to update the tables you have to send a variable to your script in the first place so MySQL knows which row in your table to update. So for example, the user clicks on a link that has a querystrig holding their ID. Then you can pass these to your SQL statement:

$ID_FROM_QUERYSTRING = $_REQUEST['NAME_OF_VARIABLE_IN_QUERYSTRING'];

$query_one = "UPDATE `subscriptions_table` SET `FIELD_NAME` = '$php_variable' WHERE `members_ID` = $ID_FROM_QUERYSTRING;

Then use the ID_FROM_QUERYSTRING in your second query also!

Hope this helps!

David
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

Post by hame22 »

thats great thanks!
Post Reply