Connecting Tech Pros Worldwide Forums | Help | Site Map

INSERT combining SELECT and php variables

bilibytes's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: Europe
Posts: 128
#1: Nov 7 '08
Hi,

i am trying to INSERT into a table lets say 4 values:
- value1
- value2
- value3
- value4

all these values are stored in variables($var1, $var2, $var3..), except one of them which is part of the final value ($var4).

$var4 may return the final value4 only if with a query to another table.
$var4 stores the name of a user.
and but I want to INSERT not the user_name, but the user_id.

"user_id" is in Table "users" and i can obtain it by doing :
Expand|Select|Wrap|Line Numbers
  1. SELECT user_id
  2. FROM users
  3. WERE user_name = $var4
but the insertion i want to do is into another table. so i would want to do for my final query:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO some_table (var1_col, var2_col, var3_col, user_id)
  2. VALUES ($var1, $var2, $var3, (
    SELECT users.user_id 
  3. FROM users
  4. WHERE users.user_name = $var4 
  5. ) )
could this work?

should i specify in the INSERT that the cols pertain to some_table like:

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO some_table (some_table.var1_col, some_table.var2_col, some_table.var3_col, some_table.user_id)
  2. VALUES ($var1, $var2, $var3, (
    SELECT users.user_id 
  3. FROM users
  4. WHERE users.user_name = $var4 
  5. ) )
or am i totally wrong?

or should i first run the query to get the user_id and then make another one to INSERT?

thankyou

bilibytes

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Nov 7 '08

re: INSERT combining SELECT and php variables


From your explanation I gathered (correct me if I'm wrong) that it is possible that value4 may not contain a value to be inserted if the user check fails. Should
that mean that the whole insert should fail? If that is the case then you need to run the user check first to determine if you need to construct the insert statement at all.
bilibytes's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: Europe
Posts: 128
#3: Nov 10 '08

re: INSERT combining SELECT and php variables


Hi, thanks for reply

No, the insertion will always be successful.
The user is authorized to do this query because he authenticated well. So i don't need to check for his credentials at this point. I only want to get the value of his id, because i don't want the names of the users to appear on the table i make the insertion into.


So my question is:

how do i make an INSERTion into a table which needs to receive a value that is located on another table.

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * User basic information
  3.  */
  4. Table 1-------------
  5. -user_id
  6. -user_name
  7.  
Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * User extra information
  3.  */
  4. Table 2------------
  5. -user_id
  6. -mood
  7. -foo
  8. -foo2
  9. ...
  10.  
the first table has the value i need (user_id) to get for the INSERTion on Table 2. I have all the information to be inserted into Table 2 except the user_id which is available from Table 1 by giving the user name.

i could do this to get my job done:

Expand|Select|Wrap|Line Numbers
  1. $resulting_user_id = "SELECT user_id FROM Table1 WHERE user_id = '$user_name'"
and then i would reuse the previos result ($resulting_user_id)to insert all the values:

Expand|Select|Wrap|Line Numbers
  1. "INSERT INTO Table2 (user_id, mood, foo, foo2) VALUES ($resulting_user_id, rest of the values already available)"
for that, i was wondering if i could make the query i posted on my first post, which shrinks all in a single one

if you have any suggestion, please let me know

Thank you

bilibytes
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Nov 10 '08

re: INSERT combining SELECT and php variables


Ok. You can put another select inside the values clause.

i.e values ('a', 'b', (select id from user where name = 'aName'))
bilibytes's Avatar
Familiar Sight
 
Join Date: Jun 2008
Location: Europe
Posts: 128
#5: Nov 10 '08

re: INSERT combining SELECT and php variables


Quote:

Originally Posted by r035198x

Ok. You can put another select inside the values clause.

i.e values ('a', 'b', (select id from user where name = 'aName'))

thank you very much!
Reply