insert ... select ... multiple cols

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
glennn3
Forum Commoner
Posts: 63
Joined: Sat Sep 20, 2003 8:43 pm

insert ... select ... multiple cols

Post by glennn3 »

you can probably guess from this what i'm tryin' to do...

Code: Select all

INSERT  INTO titles( recID, title ) 
VALUES (   'SELECT recID
FROM user
WHERE username = glennn', '$file_name' );
do i just need to do this:

Code: Select all

$query="insert into titles (recID) select recID from user where username='$username' ";
$query1="insert into titles (title) values ('$file_name');
and is there a way to combine two queries like that...?

thanks
g
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Code: Select all

$query="insert into titles (recID) select recID from user where username='$username' ";
I'm pretty sure that you cannot insert and select in the same statement. The only real logical option would be to:

Code: Select all

<?php
// select recID
$query = "SELECT recID FROM user WHERE username='$username'";

// insert
$query1="INSERT INTO titles (title, recID) VALUES ('$file_name', '$recID'); 
?>
This may not be what you are trying to accomplish, because I see no point in selecting the recID and then re-inserting it . . .
Post Reply