bushdiver
bushdiver

Reputation: 771

MySql PHP Insert with join

I can't get the following Insert to work although the syntax seems correct..

INSERT INTO views T
    JOIN members T2
        ON '$username' = T2.username 
(ITEM_ID, ITEM_TYPE, USER_ID, USER_TYPE) 
VALUES('$itemview', '$type', T2.id, '$usertype')

All the variables are predefined of course..
What am I doing wrong here?

The Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'T JOIN members T2 ON 'testuser' = T2.username (ITEM_ID, IT' at line 1

Upvotes: 1

Views: 4797

Answers (1)

Ja͢ck
Ja͢ck

Reputation: 173552

If I'm understanding you right, you want this syntax instead:

INSERT INTO views
  (ITEM_ID, ITEM_TYPE, USER_ID, USER_TYPE)
SELECT '$itemview', '$type', id, '$usertype'
FROM members
WHERE username = '$username'

It inserts a record into views with the partial contents of members. The number of rows returned with the SELECT also determines the number of inserted records; you may wish to use LIMIT if that's actually a problem.

Upvotes: 2

Related Questions