PHP Insert Form Response Into Separate MySQL Tables / Rows - php

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.

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

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.

Related

Simple way to insert foreign key automatically into a table

I have two table program(p_id(pk)AI, program_name and another table graduate_survey(id(pk)AI,total_PO1,total_PO2,session,p_id(fk). In the program table values are already inserted as
p_id Program_name
1 B.tech CSE
2 B.tech IT.
..so on. I select from a drop down list B.tech CSE and then redirect to the survey form. I enter the total_PO1, total_PO2 there and submit it.Now i want to insert in the graduate_survey table where in the fk field the p_id of B.tech CSE should automatically enter so that i can know the survey is done for which program..Is there any query in MySQL to do that? The insert operation should be done in php code.Please suggest any query.
Your approach seems to be incorrect. When you generate your dropdown menu in HTML, then the display value (the value that the user actually sees and selects) should be the program name, but the underlying value should be the id of the same record.
When you send the data to the server, then you should send the underlying id value, not the display value. Using HTML's standard dropdown menu, you would create sg like this as an output:
<select name="programs">
<option value="1">B.tech CSE</option>
<option value="2">B.tech IT</option>
</select>
If this control is submitted via a form, then the browser will send the value of the selected option automatically. If you use ajax, then your code has to retrieve the selected value of the select element. In the php code you just insert the underlying value into the graduate survey table.

how a website user interacts with a database?

I'm new to all this, and I can't seem to find the right search on google to get the answer I'm looking for. I have a front end that a user will use to create a new transaction. There is a drop down menu tied to a "categories" table that has a 1:M relationship with the "transaction" (see image). My question is, if a user submits this form to save a new transaction, they don't know the ID from the categories table, all they'll be doing is selecting one from a drop down that's pulling from the categories table. All I know how to do at this point is take their input from the drop down and store it into a field in the transaction table. I'm trying to understand how to use relationships, but in my limited understanding of how keys work, it seems that unless you knew what the ID number was, then the relationship does no good. If all you're doing is taking the submission from the drop down and storing it in the transaction table, then you've still just got the category name stored in 2 different tables, so what's the point? I'm missing a step in here somewhere and I'm hoping someone can help make sense out of this.
The user wouldn't know the Category ID. (Though they could if they wanted to. It's in the markup.) More importantly, your code would know the Category ID. When creating that drop down list (the select element), your code would be populating it with both the ID and the Name. The ID is used for the value, the Name is used for the display text.
The resulting markup might look like this:
<select name="category">
<option value="1">First Category</option>
<option value="2">Second Category</option>
<option value="3">Third Category</option>
</select>
And so on.
When the user selects one, they're choosing based on the text. But the actual form will post the value of the selected option back to the server. So if the user selects Second Category then in your server-side code $_POST["category"] will contain the value 2, which is the ID you need to insert into your Transaction record.

Showing selected country in a dropdown using PHP

On my page I have a dropdown menu with a list of countries, this is a page for users to update their address details so I would like to have the "selected" value when they visit the page set to what the entered upon signup.
I have
<option value="254" >Afghanistan</option><option value="255" >Albania</option>
Etc as my HTML markup and in the database the user's country is stored as the code like in the markup.
Normally this would be simple but as it is such a vast list of countries I can't write php code in each one manually.
Thanks
You tagged the question as MySQL.
The answer is there:
Make a table called countries
Table countries
---------------
id integer auto_increment
name varchar(255)
code char(3)
....
And run a query like
SELECT name FROM countries ORDER BY name
Use the output in your dropdown.
If you are using PHP to create the drop-down, simply insert the "select" test into the loop. (If you aren't, then you might want to - create an array of country vals or get from DB and create the options with a PHP loop.)

AJAX Request that fetches ID as well and puts that into the HTML

I'm trying to make an Ajax search form - This form will simply search the database for names.
The table design is as follows:
id name age
1 some name 10
2 some name2 11
3 some name3 12
Each name is associated with a unique ID.
I want to create a drop down menu of top hits when the user enters their search string.
Also, somehow in the background i would like to retrieve the associated ID with each result as well. That ID will be POST'ed to a form say
myform.php
Now, my question is as follows:
1) How do i create the drop down menu?
I believe i can follow this tutorial and simply customise it to create a drop down menu.
http://www.w3schools.com/ajax/ajax_aspphp.asp
This would be fairly simple.
2) The main question is, how do i keep track of the ID and selected name, so that i can POST that ID to myform.php
Thanks.
<select name="carlist">
<option value="id">name</option>
</select>
Keep id and name like this.
on posting id is posting , you can get name from id .
I hope this is what you are asking.
But i dont find drop down in that link you provide

Making a checkbox recognize its intended data

Level: PHP Learner
I'm stuck with a checkbox problem. I have a db that contains names and unique id numbers.
Using a query, I am pulling a selection of students and showing them to a user in an ultra simple HTML table on a form. Each row begins with a checkbox. The method is POST. So far, so good. My table looks like this:
+-----------+----------+----------+
| SELECT | NAME | ID |
+-----------+----------+----------+
| [] | John | 2233 |
+-----------+----------+----------+
| [] | Susie | 5577 |
+-----------+----------+----------+
[-SUBMIT-]
My problem is that I cannot seem to make the checkbox associate with each record's unique ID. Once the user has selected rows and clicked submit, the $_POST array remains empty.
None of my beginners books reference this specific issue. They go through the "regular" checkbox routines that don't involve interacting with rows from a db. I also could not find an issue on Stackoverflow that addresses this. Also tried Google: plenty of stuff on checkboxes, but I couldn't find any that helped me on this problem.
Just do something like this:
<input type="checkbox" name="ids[]" value="2233" /> ... rest of row
<input type="checkbox" name="ids[]" value="5577" /> ... rest of row
Now, in PHP, you can get the selected ids like this:
$ids = $_POST['ids'];
if( empty($ids) ) $ids = array();
This sets $ids to an empty array if the form was submitted without any of the checkboxes checked.
Edit: If your form doesn't get more complex as you describe, take Doug Neiner's approach, as it is way simpler. This approach is right if a table is likely to have a number of columns.
I like to do it this way:
Number the checkboxes sequentially (1 to 100) and add a hidden field connecting the row number to a real database ID:
<input type="checkbox" name="row_1" value="checked">`
<input type='hidden' name='row_1_id' value='2233'>`
Store the total number of rows in another hidden field
<input type='hidden' name='row_total' value='99'>
Then, in the receiving script, iterate from 1 to the total number of rows using for, check whether this row was selected, and get the associated database ID:
for ($i = 1; $i <= $number_of_rows; $i++)
{
if ($_POST["row_$i"] == "checked")
{
$database_id_unsafe = $_POST["row_{$i}_id"];
...
the latter, of course, needs to be properly sanitized and escaped in case it is processed further.

Categories