473,666 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

INSERT combining SELECT and php variables

bilibytes
128 New Member
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 11241
r035198x
13,262 MVP
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 New Member
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_use r_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 MVP
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 New Member
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
2415
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 this Insert into article values(now(), now(),....)
3
2329
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 combining them. That is, the following doesn't work properly (not in all emaxen, at least): #!/usr/bin/env python
1
8734
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware tblSoftware ------------------- ------------------ ---------------------- PID PName PID* SID* SID SWName --- ----- --- --- --- ------ 1 Thomas 1 1 ...
3
1920
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? E.G. 2 Entites/Tables, tblA and tblB. Both have the same Attributes/fields 'Fname' and 'Lname. So my SQL statement looks like this:- "INSERT INTO tblB (Fname, Lname) SELECT tblA.Fname FROM tblA
6
1581
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 seem to read the values from Table B... <CODE BEGIN INSERT INTO TableA (LocationID, SeatID, SeatNumber, DateScheduled) VALUES (1, 12, 109, 9/26/2007)
4
5104
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 passed 2 variables from a form; $word and $def and i'm inserting them ok but i'm having trouble getting and passing on their ID's in order to insert them into the relation table 's' I've checked every query and they all work individually, so i...
2
1601
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 FHWA_StandSamp_2008(LRS_ID_NEW) values('0402000010') where LRS_ID = '0403700010'") Traceback (most recent call last): File "<pyshell#37>", line 1, in ?
2
3198
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 with the javascript im using but i can print the correct values so why am i unable to use them to enter data to a database? sorry i am new to all this here is the updatedata.php file <?php session_start(); $host="localhost"; // Host...
3
2834
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 dataset to data in a second dataset, using a common key. I will first describe the problem in words and then I will show my code, which has most of the solution done already. I have built an ASP.NET that queries an Index Server and returns a...
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6189
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4358
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.