473,503 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can I update one table with values from another table?

5 New Member
I want to make a poll for my website(flash) where I can change the question from time to time. When I put up a new question, question_id in table1 will change (auto increment)

I am trying to make an updatefunction in php to work with amfphp and flash.

I have two tables:
1: Question: question_id, question.
2: Answer: answer_id, answer, question_id

question_id is foreign key in table 2. Can I somehow update question_id in table 2 with the 'most recent or higest' value from question_id in table 1.

I hope you understand my question, I find it very difficult to explain what I am looking for.

Any help is much appreciated, Thank you.

Karla
May 12 '10 #1
5 1978
dlite922
1,584 Recognized Expert Top Contributor
I don't understand your question.

If it's a poll, then you provide the question (table1) and the user taking the poll provides records in answer table (table2).

You're program should know which question the user is answering to tie that back to table 1, so all you have to do is make sure you have the most recent question.

Which is easy to do if the latest question has the highest question_id

SQL:
SELECT * FROM question_table ORDER BY question_id DESC LIMIT 1;


Feel free to elaborate more,




Dan
May 12 '10 #2
karla007
5 New Member
I see I have a long way to go when I can't even explain my problem. I am not even sure I am looking for an UPDATE.
At first my poll worked just fine (I only have one question and you can only answer yes or no). Everytime the user answered yes, the function just inserted yes into the answer table: INSERT INTO answer(answer) VALUES('yes');
The problems started when I wanted to add a new question. To make sure that the new answers only belonged to the new question, I added a foreing key to the answer table: question_id. So now my INSERT must look like this: INSERT INTO answer(answer, question_id) VALUES('yes', some variable question_id that comes from the question table). I don't know how to get that variable into VALUES, that's when I thought I needed an UPDATE instead, but that won't work either.

Does this make any sense at all?
I am totally new to this, but I really want to learn.

Thank you for your time
Karla
May 12 '10 #3
karla007
5 New Member
@karla007
After a good nights sleep and some more research I found the solution:
INSERT INTO answer (answer, question_id) VALUES ('yes', (SELECT MAX(question_id) FROM question));

It has taken me two days to find out!
May 13 '10 #4
dlite922
1,584 Recognized Expert Top Contributor
that variable should be pushed to the query just like the answer.

So basically the question_id is in a hidden form field along with the answer.

When it gets submitted with answer, you just provided in the VALUES. There is no need for the MAX(question_id)

Ask yourself this. What if you have multiple questions displayed at the same time, how do you know which answer goes to which question?

The solution: submit the question_id with every question's answer.

Hope that makes sense,






Dan
May 13 '10 #5
dlite922
1,584 Recognized Expert Top Contributor
@karla007
You have hard coded 'yes' into that query? isn't that actually coming from the user? How do you know the user answered 'Yes'? what about "no"?

What if the answer is not yes/no, what if it's A,B,C or a number?





Dan
May 13 '10 #6

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

Similar topics

0
1631
by: Chris Nighswonger | last post by:
------=_NextPart_000_0013_01C352C0.6B0A6E30 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi All, Is it possible in MySQL to use UPDATE to update...
9
19595
by: Deja User | last post by:
This is probably very simple but I can't figure out a way to update one table from another table. Here is an example: ------ TABLEA ------ first_name last_name
1
31097
by: Mike9900 | last post by:
What is the best way to copy DataRow from one table to another table, without copying its structure, which means copying only its data. -- Mike
1
1722
by: thengfen | last post by:
Hi! Im having a problem in transfering a set of records from a table to another table. The scenario is when i select combo box (Course taken such as diploma in IT), then the process will...
1
2227
by: satishkhachane | last post by:
-------------------------------------------------------------------------------- I want to select one or more columns from one typed datatable and add it to another table . how I can do this ? ...
1
1982
by: Sakakini | last post by:
How can I append last entry from one table to another table???
4
4832
by: xoozlez | last post by:
Hi there, How do I insert new records from a dbo table to another table? This is what I have: 1 dbo_company 1 Member (table) I made a query in dbo_company with the criteria I only want to...
2
2228
by: tomash | last post by:
Hi! I ve got two tables in Access 2007. I want to update a field of DataTable from another table, DataSumTable when two of their fields equals. ( the fields : Name and Period) I tried this...
3
2573
by: anil2083 | last post by:
How to migrate the comma separated values from one table to another table? suppose we have table i.e XYZ and we have comma separated values in few columns i.e( column_name and values are...
0
7202
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,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
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,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4673
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...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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 ...

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.