Help - Search - Members - Calendar
Full Version: Query ? Insert Data Into 2 Tables Together
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
MrTouz
Here is my problem,

I have a form to register. When you register is sends data into my table called USERS. It holds Username, Timestamp, Email, ID.
It works its fine !

The thing is i have an other table with the users INFO. called USERS_INFO. What i want to do is while registering it creates the USERNAME (primary key on both tables) on my table USERS_INFO.

So basically when you register it sends data into USERS & USERS_INFO. All the info such as email... timestamp... into the USERS and only the USERNAME into the the table USERS_INFO.

I really don't know what my query should look like ! :/
Jetteh22
Create two different queries is what I would do.

First create the one that just send the USERNAME into USERS_INFO since it's shorter.

Then do the other one that sends all the other information.
MrTouz
I tried that, somehow did not work. I am scared. Lol

Thanks a lot ! im still trying different ways.
Bogey
How does that not work? It doesn't make sense to me.
CODE
$sql[] = "INSERT INTO users VALUES (value1, value2, value3,...)";
$sql[] = "INSERT INTO users_info VALUES (value1, value2, value3,...)";
foreach($sql as $val)
{
       mysql_query($val) or die(mysql_error() ." on line <strong>". (__LINE__) ."</strong> in file <strong>". (__FILE__) ."</strong>");
}

Or you could do it separately.
CODE
$sql = "INSERT INTO users VALUES (value1, value2, value3,...)";
mysql_query($sql) or die(mysql_error() ." on line <strong>". (__LINE__) ."</strong> in file <strong>". (__FILE__) ."</strong>");
$sql = "INSERT INTO users_info VALUES (value1, value2, value3,...)";
mysql_query($sql) or die(mysql_error() ." on line <strong>". (__LINE__) ."</strong> in file <strong>". (__FILE__) ."</strong>");

Hope this helps.
NDBoost
the array method and foreach() is obviously the better methor here.. that way you only have to worry about ONE mysql_query that ever needs to be changed
MrTouz
Well i tried, definitely not your structure Bogey, i was probably thinking an other way. But i find my way to solve my issue. It made it better after all.

Thank you for your help.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.