Problem with MYSQL INSERT SELECT

I have a little difficulty understanding how to do an INSERT SELECT.

For example, I have two tables.

TABLE : users  

 id | name   | gender  
 1  | John   | m  
 2  | Mary   | f  

TABLE : website  

 fid | url             | id  
 1   | www.desilva.biz | 2  
 2   | gidhelp.com     | 4  

Now let's say I want to add another query to the table website. I get two variables, say:

$user = John;
$site = "www.google.com";

I want to select the John table identifier from users and paste it into the website table in one statement.

How can i do this?

+5
source share
1 answer

Assuming your variables are already successfully escaped and not SQLinjectable:

INSERT
INTO    website (url, fid)
SELECT  $site, id
FROM    users
WHERE   name = $user
+8
source

All Articles