Forum Moderators: coopster

Message Too Old, No Replies

Insert value into several tables at same time?

Want to insert a username into lots of tables

         

s9901470

9:05 am on Jul 28, 2005 (gmt 0)

10+ Year Member



Hi

I'd like to insert someone's username into multiple tables when they register

e.g. insert $uname into the tables:
users, orders, complaints, requests

Can anyone tell me how to write this in mysql?

Many thanks

DanA

9:33 am on Jul 28, 2005 (gmt 0)

10+ Year Member



having a field named user_name in users, orders...
INSERT INTO `users` (`user_name`) VALUES ('$uname');
INSERT INTO `orders` (`user_name`) VALUES ('$uname');
and so on

[edited by: DanA at 9:33 am (utc) on July 28, 2005]

omoutop

9:33 am on Jul 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi!

try this:

$query1 = "insert into users (username) VALUES ('".$uname."')";
$result1 = mysql_query($query1);

$query2 = "insert into orders (username) VALUES ('".$uname."')";
$result2 = mysql_query($query2);

$query3 = "insert into requests (username) VALUES ('".$uname."')";
$result3 = mysql_query($query3);

u have to check if submissions of data are correct
i.e

if (!$query3)

{
echo 'error';
}

else
{
echo 'ok';
}

arran

11:39 am on Jul 28, 2005 (gmt 0)

10+ Year Member



If you are using MySQL 5.0.2+, a more efficient solution (i.e. only 1 database call) would involve the use of triggers [dev.mysql.com]

s9901470

9:28 am on Jul 29, 2005 (gmt 0)

10+ Year Member



Could I modify this to insert two lines into each table?

That is, can I combine the two seperate statements below into one line?

$query1 = "insert into table1 (uname,attempt1) VALUES ('".$uname."','1')";
$result1 = mysql_query($query1);

$query2 = "insert into table1 (uname,attempt2) VALUES ('".$uname."','2')";
$result2 = mysql_query($query2);

omoutop

9:54 am on Jul 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i dont really understand what u r trying to do...
u can try combining the two statement by creating a loop...pseudocode would be like this:

$i=1
while ($i=2)
{
$query1 = "insert into table($i) (uname,attempt($i)) VALUES ('".$uname."','$i')";
$result1 = mysql_query($query1);

}
$i++

u gotta search the syntax to make it work....dont konw if this is exatly what u want...

s9901470

10:34 am on Jul 29, 2005 (gmt 0)

10+ Year Member



I want to insert two rows for each person when they register, but change the value of one of the variables so that each person has one row '1' and another '2'

omoutop

10:50 am on Jul 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



u can try the code i sent you before...with the loop..but u gotta search the syntax a bit...