0

I have a form that, upon being submitted via POST, needs to be inserted into two separate tables based on the field. Each field needs to be added as its own line.

Imagine a survey; there are four sections total. For each question within a section, the user selects a value between 1 and 5. There is also an optional notes text area at the bottom of each section.

Each question has its own unique ID in the database in a "questions" table. These questions contain the ID of the section ("sections" table) it belongs to for reference.

Question 1:

How can I insert each answer as its own row in a table called "answers" with the ID of the question?

The structure for "answers" looks like:

id (AI) | question_id | value (user submitted, 1-5) | response_id

Question 2:

How can I then insert each note for each section into a table called "notes" with the id of each section?

The structure for "notes" looks like:

id (AI) | section_id | value (user submitted) | response_id

Response_id is the resulting ID of inserting the user's response into a table called "responses." This table ties it all together for outputting the results for each user submitted response.

Thanks in advance.

3
  • These just sound like relatively simple queries .. what are you stuck on? Commented Feb 8, 2013 at 17:45
  • you can run multiple different queries in a single script. even connection to multiple different dbs, etc... if you can insert into one db, you can insert into many. Commented Feb 8, 2013 at 17:50
  • @explosion pill I'm hung up on separating the responses. The queries are easy, but for whatever reason I'm not understanding how to separate the post responses in order to generate the queries. Commented Feb 8, 2013 at 18:08

2 Answers 2

1

You might want to use an array to keep track of the ids you need. Your HTML block should look something along the lines of:

<h3>Question '.$questionId.': Your question title here</h3>
<input type="hidden" name="question_id" value="'.$questionId.'" /> //Not necessary, but just to show you that a hidden field can be useful too 
<input type="text" name="answer['.$questionId.']" />

Then you just use the $_POST data as a regular array, in a for loop or in a big query (better performance depending on the amount of fields/data you are dealing with).

More info on multidimensional arrays in this post: Submitting a multidimensional array via POST with php

Sign up to request clarification or add additional context in comments.

Comments

1

You can do one thing like this may be. You said you have question id for each question right so when user selects answer give name to each question like this

name = "question[<?php echo $question_id ?>]"

<select name="cars">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>

For the not just give simple name. When user submits it take the entire question[$ids] array loop through each id and insert into answers table using question id. You need to little bit of array operations.

For Notes you just use section id for in the name field and insert into database normally.

2 Comments

Thanks! This got me heading in the right direction. Coding away now.
if this help you to write code for your answer pelase upvote it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.