Contact Us Forum Rules Search Archive
WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-15-2007, 02:17 PM
WebProWorld New Member
 

Join Date: Jun 2007
Posts: 4
davss RepRank 0
Question Multiple insert into more than one mySQL table

Hi!

I want to insert data into two tables in one mysql query. First one would store user account details in TBL_USERS then i want on successful insertion of the primary a new record to be inserted at the same time into another table containing addresses TBL_USERS_DATA with just the username and time stamp (they will be able to add more details later on). Is this the best solution? I would like to keep login accounts/details separated from addresses thereby speed up queries etc.

function addNewUser($username, $password, $email){
$time = time();
/* If admin sign up, give admin user level */
if(strcasecmp($username, ADMIN_NAME) == 0){
$ulevel = ADMIN_LEVEL;
}else{
$ulevel = USER_LEVEL;
}

$q = "INSERT INTO ".TBL_USERS." VALUES ('$username', '$password', '0', $ulevel, '$email', $time)";

return mysql_query($q, $this->connection)
}

This is what i want to be inserted simultaneously :

$x = "INSERT INTO ".TBL_USERS_DATA." (username, timestamp) VALUES ('$username', $time)";

I have found some info at Learning Journal - Inserting Data in Multiple Tables about using mysql_insert_id() function but not sure how to implement it into mine.

Thanks for all help.
Reply With Quote
  #2 (permalink)  
Old 06-15-2007, 06:37 PM
DaveSawers's Avatar
WebProWorld Veteran
 

Join Date: Dec 2006
Location: Calgary, Alberta, Canada
Posts: 389
DaveSawers RepRank 1
Default Re: Multiple insert into more than one mySQL table

Quote:
Originally Posted by davss View Post
I want to insert data into two tables in one mysql query.
Tough. You should use two queries. There's no advantage to be gained by trying to do it in one.

Quote:
Originally Posted by davss View Post
I have found some info at Learning Journal - Inserting Data in Multiple Tables about using mysql_insert_id() function but not sure how to implement it into mine.
The mysql_insert_id function returns the value of the id automatically generated in the previous insert. To make it work, you need an AUTOINCREMENT field in the first table which you can then use to tie the fields together in the second table. So, if your first table was created with a field defined something like:

id integer autoincrement primary key

and if your second table has something like:

id integer autoincrement primary key
id_tbl_users integer

then you can set the id_tbl_users in the second query to be equal to the result returned from mysql_insert_id after completing the first insert query
__________________
Dynamic Software Development
www.activeminds.ca
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Tags: , , , ,



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need php/mySQL solution for multiple domains spiderbait Web Programming Discussion Forum 2 01-17-2006 11:05 PM
Table layout using php/mysql to extract golf statistics daddyg Web Programming Discussion Forum 8 08-18-2005 02:43 AM
CSS Table List - PHP mySQL richkoi Graphics & Design Discussion Forum 4 06-06-2005 04:09 AM
PHP MySQL Multiple Column Dynamic Dropdown Chris W.P. Web Programming Discussion Forum 0 08-19-2004 01:34 PM
[PHPmySQL] How? Make sortable table format of mySQL database MasterT Database Discussion Forum 1 06-08-2004 11:35 PM


Search Engine Optimization by vBSEO 3.2.0