473,382 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

INSERT combining SELECT and php variables

bilibytes
128 100+
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
Nov 7 '08 #1
4 11220
r035198x
13,262 8TB
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.
Nov 7 '08 #2
bilibytes
128 100+
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
Nov 10 '08 #3
r035198x
13,262 8TB
Ok. You can put another select inside the values clause.

i.e values ('a', 'b', (select id from user where name = 'aName'))
Nov 10 '08 #4
bilibytes
128 100+
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!
Nov 10 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bob Bedford | last post by:
I've a query with insert records in a table. I've 2 fields: DateTimeInsertion and DateTimeLastModification. When I create the record, I must have those 2 fields set the same value. Now I've...
3
by: Magnus Lie Hetland | last post by:
If I want to specify both mode and source encoding using the -*- FOO -*- syntax in emacs -- how can I do that? Both insist on being on the second line of the file, yet I have found no way of...
1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware ...
3
by: Kelvin | last post by:
Is it possible when coding a SQL statement to use variables within an INSERT INTO statement i.e the variables contain some data I wish to insert along with the standard entity.attribute data? ...
6
by: ArizonaState | last post by:
I am trying to do 2 inserts in a stored proc. One is straight forward. The other is an insert into Table A from values in Table B. Here is my code. While the first part works well it does not...
4
bugboy
by: bugboy | last post by:
I'm inserting a new word into table 'w' and a definition into table 'c' which are linked in table 's' which is the relation table for the many to many relationship between 'w' and 'c'. I've been...
2
by: Greg Corradini | last post by:
Hello, I've never gotten this traceback error before using mx.ODBC. Any ideas about resolving this issue? The statement and the error it generates are listed below. curse.execute("Insert into...
2
by: adamace5o | last post by:
When i try to use post variables with php and mysql i can't get the insert into statement to accept varibles as values. If i use 'test' instead of $test it does work. I suspect it is something to do...
3
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.