Your question doesn't exactly make sense. It sounds like you want to insert into one table (Products
) and one of the fields is also in another table (User.username
). Where does the rest of the data for Products
come from? Can't you just insert the username directly?
Insert into products (name, price, username) values (?, ?, ?)
Use whatever mechanism you want to pass in the data to that SQL statement.
If, however, you actually have a third table, say, 'Prod_for_sale
' and you want to insert a bunch of data into that table, from both Users
and Products
, you can do something like this (as suggested by Rick J, but this example has two tables):
insert into products for sale (username, user_country, product_name, product_price)
select u.username, u.country, p.name, p.price
from products p, users u
where p.id = ?
and u.username = ?
Then you can copy whatever data you want from the tables in question into the destination table.
A word of advice though: you should be using a user ID instead of a username to identify the user's data in the database. This will allow you to change your username policy later or allow for usernames to be updated, etc. Generally try to make all your identifiers numbers and you'll save yourself hassle in the future.