SQL Select inside Insert

I have following.

.
.
.
$userid = 2
.
$query = "select username from users where userid = ".$userid.";";
.
$username = $line['uid'];
$data="Some Data Here";
.
$query = "insert into logger (username, data) valuse ($username, $data);";
.
 

I would like to not have 2 database calls.

Im sure there is a way to "join" the 2. I want to store the actual username in the logger table and not the UID#, I have not had much luck.

Probabally something really easy im missing.

Something like this:

insert into logging 
('type','who','data','ipaddress') 
values 
("testing", (select username from users where id = 2;), "data here","1.1.1.1");

Thanks...

I figured it out...

insert into logging 
(type,who,data,ipaddress) 
values 
("testing",(select username from users where id = 2),"data here","1.1.1.1");

Was using wrong quotes.

In MySQL there is a INSERT ... SELECT that you can probably use to avoid a subquery, but you didn't confirm whether you were using MySQL. Not sure about other database engines though.

Yes I am using MySQL v5.0.45