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
inserting values in to multiple tables
Moderator: General Moderators
-
bigdavemmu
- Forum Newbie
- Posts: 22
- Joined: Wed Oct 26, 2005 4:01 am
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
$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