Hi everyone,
I have created these two tables.
create table USERS (
userID int unsigned not null primary key auto_increment,
user_name varchar (50),
user_surname varchar (50)
) type=InnoDB;
create table LOGIN (
loginID int unsigned not null primary key auto_increment,
userID int unsigned,
login_name varchar (50),
login_password varchar (50)
) type=InnoDB;
Now I would like to insert values into the users table, store the userID into a variable and pass the value
in the related field of the login table.
Here is my transaction
BEGIN WORK;
INSERT INTO USERS VALUES ('null', 'user_name', 'user_surname');
select @myvar:=last_insert_id();
INSERT INTO LOGIN VALUES ('null', '@myvar', 'login_name', 'login_password');
COMMIT;
If I run it, it does not give any error but, when I open the 'Login' table, only 0 have been stored in the userid field
and not the correct value from the 'users' table.
If I run this transaction in the prompt it shows the correct value stored in the variable so I assume
that there is something wrong in the way I recall the variable.
Can anyone tell me what I am doing wrong?
Thank you
Qatqat
I have created these two tables.
create table USERS (
userID int unsigned not null primary key auto_increment,
user_name varchar (50),
user_surname varchar (50)
) type=InnoDB;
create table LOGIN (
loginID int unsigned not null primary key auto_increment,
userID int unsigned,
login_name varchar (50),
login_password varchar (50)
) type=InnoDB;
Now I would like to insert values into the users table, store the userID into a variable and pass the value
in the related field of the login table.
Here is my transaction
BEGIN WORK;
INSERT INTO USERS VALUES ('null', 'user_name', 'user_surname');
select @myvar:=last_insert_id();
INSERT INTO LOGIN VALUES ('null', '@myvar', 'login_name', 'login_password');
COMMIT;
If I run it, it does not give any error but, when I open the 'Login' table, only 0 have been stored in the userid field
and not the correct value from the 'users' table.
If I run this transaction in the prompt it shows the correct value stored in the variable so I assume
that there is something wrong in the way I recall the variable.
Can anyone tell me what I am doing wrong?
Thank you
Qatqat