PHP loop through form table and submit data to database












0















Hoping someone can assist with this, been looking at it since the weekend.
I have a form which contains 3 fields (Name, Email, Quarter) and a table with multiple questions (I reduced the code to just 2 questions for illustration purposes.



I want the 3 fields (Name, Email, Number) to be written to the database with question 1 in row 1 and with question 2 in row 2.



Problem is my code only writes the 3 fields (Name, Email, Number) to row 1 (question 1) and writes blanks to row 2 for the 3 fields (Name, Email, Number), the table fields write successfully. What am i doing wrong?



Database View



Name Email Number KPICode Response Compliant Comments
Joe Joe@test.com 555 k21 yes yes test
554 k45 no no test



HTML Code:



        <div class="item form-group">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="hfname">Name</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="name" name="name" class="form-control col-md-7 col-xs-12">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="email">Email:</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<Select type="email" id="email" name="email" class="form-control col-md-7 col-xs-12"></select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="quarter">Quarter:</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select id="quarter" name="quarter" class="form-control">
<option>Choose option</option>
<option>Q1</option>
<option>Q2</option>
<option>Q3</option>
<option>Q4</option>
</select>
</div>
</div>
</div>
<table class="table table-bordered">
<thead class="headings">
<tr>
<th>Code</th>
<th>Category</th>
<th>Performance Indicator</th>
<th>Response</th>
<th>Compliance</th>
<th>Notes</th>
</tr>
</thead>

<tbody>
<tr>
<td id="env1" name ="code" type="text" scope="row">ENV1</td>
<td>1.1 KPI</td>
<td>KPI Description.</td>
<td><select id="response" name="response">
<option>Yes</option>
<option>No</option>
</select>
</td>
<td><select id="Compliance" name="compliance">
<option>Compliant</option>
<option>Partially Compliant</option>
<option>Non-Compliant</option>
</select></td>
<td><input id="notes" name="notes" role="row" Type="text"></td>
</tr>
<tr>

<th id="env2" name="code" scope="row">ENV2</th>
<td>1.2 KPI</td>
<td>KPI Description.</td>
<td><select id="response" name="response">
<option>Yes</option>
<option>No</option>
</select>
</td>
<td><select id="Compliance" name="compliance">
<option>Compliant</option>
<option>Partially Compliant</option>
<option>Non-Compliant</option>
</select></td>
<td><input id="notes" name="notes" role="row" Type="text"></td>
</tr>
</tbody>
</table>


My php code to write the info to the db:



foreach($_POST['response'] as $index => $val)
{
$response = $val;
$rfname = $_POST['name'][$index];
$HospitalName = $_POST['email'][$index];
$quarter = $_POST['quarter'][$index];
$kpicode = $_POST['code'][$index];
$compliance = $_POST['compliance'][$index];
$notes = $_POST['notes'][$index];

$sql = "INSERT INTO InfoForm (Name, Email, Quarter, KPICode, Response,
Compliance, Notes)
VALUES ('$name', '$email', '$quarter', '$kpicode', '$response',
'$compliance', '$notes')";
$result = mysqli_query($conn, $sql);
}









share|improve this question

























  • Your HTML as posted doesn't have the response, code, compliance or notes fields. Can you share more of the HTML? Without the full context we can't really answer the question.

    – cincodenada
    Nov 20 '18 at 22:02






  • 1





    Output the POST (print_r or var_dump) and see what you are actually working with. You are open to SQL injections as well

    – user3783243
    Nov 20 '18 at 22:03













  • html code updated with missing fields

    – PoolHall Junkie
    Nov 20 '18 at 22:23
















0















Hoping someone can assist with this, been looking at it since the weekend.
I have a form which contains 3 fields (Name, Email, Quarter) and a table with multiple questions (I reduced the code to just 2 questions for illustration purposes.



I want the 3 fields (Name, Email, Number) to be written to the database with question 1 in row 1 and with question 2 in row 2.



Problem is my code only writes the 3 fields (Name, Email, Number) to row 1 (question 1) and writes blanks to row 2 for the 3 fields (Name, Email, Number), the table fields write successfully. What am i doing wrong?



Database View



Name Email Number KPICode Response Compliant Comments
Joe Joe@test.com 555 k21 yes yes test
554 k45 no no test



HTML Code:



        <div class="item form-group">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="hfname">Name</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="name" name="name" class="form-control col-md-7 col-xs-12">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="email">Email:</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<Select type="email" id="email" name="email" class="form-control col-md-7 col-xs-12"></select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="quarter">Quarter:</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select id="quarter" name="quarter" class="form-control">
<option>Choose option</option>
<option>Q1</option>
<option>Q2</option>
<option>Q3</option>
<option>Q4</option>
</select>
</div>
</div>
</div>
<table class="table table-bordered">
<thead class="headings">
<tr>
<th>Code</th>
<th>Category</th>
<th>Performance Indicator</th>
<th>Response</th>
<th>Compliance</th>
<th>Notes</th>
</tr>
</thead>

<tbody>
<tr>
<td id="env1" name ="code" type="text" scope="row">ENV1</td>
<td>1.1 KPI</td>
<td>KPI Description.</td>
<td><select id="response" name="response">
<option>Yes</option>
<option>No</option>
</select>
</td>
<td><select id="Compliance" name="compliance">
<option>Compliant</option>
<option>Partially Compliant</option>
<option>Non-Compliant</option>
</select></td>
<td><input id="notes" name="notes" role="row" Type="text"></td>
</tr>
<tr>

<th id="env2" name="code" scope="row">ENV2</th>
<td>1.2 KPI</td>
<td>KPI Description.</td>
<td><select id="response" name="response">
<option>Yes</option>
<option>No</option>
</select>
</td>
<td><select id="Compliance" name="compliance">
<option>Compliant</option>
<option>Partially Compliant</option>
<option>Non-Compliant</option>
</select></td>
<td><input id="notes" name="notes" role="row" Type="text"></td>
</tr>
</tbody>
</table>


My php code to write the info to the db:



foreach($_POST['response'] as $index => $val)
{
$response = $val;
$rfname = $_POST['name'][$index];
$HospitalName = $_POST['email'][$index];
$quarter = $_POST['quarter'][$index];
$kpicode = $_POST['code'][$index];
$compliance = $_POST['compliance'][$index];
$notes = $_POST['notes'][$index];

$sql = "INSERT INTO InfoForm (Name, Email, Quarter, KPICode, Response,
Compliance, Notes)
VALUES ('$name', '$email', '$quarter', '$kpicode', '$response',
'$compliance', '$notes')";
$result = mysqli_query($conn, $sql);
}









share|improve this question

























  • Your HTML as posted doesn't have the response, code, compliance or notes fields. Can you share more of the HTML? Without the full context we can't really answer the question.

    – cincodenada
    Nov 20 '18 at 22:02






  • 1





    Output the POST (print_r or var_dump) and see what you are actually working with. You are open to SQL injections as well

    – user3783243
    Nov 20 '18 at 22:03













  • html code updated with missing fields

    – PoolHall Junkie
    Nov 20 '18 at 22:23














0












0








0








Hoping someone can assist with this, been looking at it since the weekend.
I have a form which contains 3 fields (Name, Email, Quarter) and a table with multiple questions (I reduced the code to just 2 questions for illustration purposes.



I want the 3 fields (Name, Email, Number) to be written to the database with question 1 in row 1 and with question 2 in row 2.



Problem is my code only writes the 3 fields (Name, Email, Number) to row 1 (question 1) and writes blanks to row 2 for the 3 fields (Name, Email, Number), the table fields write successfully. What am i doing wrong?



Database View



Name Email Number KPICode Response Compliant Comments
Joe Joe@test.com 555 k21 yes yes test
554 k45 no no test



HTML Code:



        <div class="item form-group">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="hfname">Name</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="name" name="name" class="form-control col-md-7 col-xs-12">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="email">Email:</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<Select type="email" id="email" name="email" class="form-control col-md-7 col-xs-12"></select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="quarter">Quarter:</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select id="quarter" name="quarter" class="form-control">
<option>Choose option</option>
<option>Q1</option>
<option>Q2</option>
<option>Q3</option>
<option>Q4</option>
</select>
</div>
</div>
</div>
<table class="table table-bordered">
<thead class="headings">
<tr>
<th>Code</th>
<th>Category</th>
<th>Performance Indicator</th>
<th>Response</th>
<th>Compliance</th>
<th>Notes</th>
</tr>
</thead>

<tbody>
<tr>
<td id="env1" name ="code" type="text" scope="row">ENV1</td>
<td>1.1 KPI</td>
<td>KPI Description.</td>
<td><select id="response" name="response">
<option>Yes</option>
<option>No</option>
</select>
</td>
<td><select id="Compliance" name="compliance">
<option>Compliant</option>
<option>Partially Compliant</option>
<option>Non-Compliant</option>
</select></td>
<td><input id="notes" name="notes" role="row" Type="text"></td>
</tr>
<tr>

<th id="env2" name="code" scope="row">ENV2</th>
<td>1.2 KPI</td>
<td>KPI Description.</td>
<td><select id="response" name="response">
<option>Yes</option>
<option>No</option>
</select>
</td>
<td><select id="Compliance" name="compliance">
<option>Compliant</option>
<option>Partially Compliant</option>
<option>Non-Compliant</option>
</select></td>
<td><input id="notes" name="notes" role="row" Type="text"></td>
</tr>
</tbody>
</table>


My php code to write the info to the db:



foreach($_POST['response'] as $index => $val)
{
$response = $val;
$rfname = $_POST['name'][$index];
$HospitalName = $_POST['email'][$index];
$quarter = $_POST['quarter'][$index];
$kpicode = $_POST['code'][$index];
$compliance = $_POST['compliance'][$index];
$notes = $_POST['notes'][$index];

$sql = "INSERT INTO InfoForm (Name, Email, Quarter, KPICode, Response,
Compliance, Notes)
VALUES ('$name', '$email', '$quarter', '$kpicode', '$response',
'$compliance', '$notes')";
$result = mysqli_query($conn, $sql);
}









share|improve this question
















Hoping someone can assist with this, been looking at it since the weekend.
I have a form which contains 3 fields (Name, Email, Quarter) and a table with multiple questions (I reduced the code to just 2 questions for illustration purposes.



I want the 3 fields (Name, Email, Number) to be written to the database with question 1 in row 1 and with question 2 in row 2.



Problem is my code only writes the 3 fields (Name, Email, Number) to row 1 (question 1) and writes blanks to row 2 for the 3 fields (Name, Email, Number), the table fields write successfully. What am i doing wrong?



Database View



Name Email Number KPICode Response Compliant Comments
Joe Joe@test.com 555 k21 yes yes test
554 k45 no no test



HTML Code:



        <div class="item form-group">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="hfname">Name</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" id="name" name="name" class="form-control col-md-7 col-xs-12">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="email">Email:</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<Select type="email" id="email" name="email" class="form-control col-md-7 col-xs-12"></select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="quarter">Quarter:</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select id="quarter" name="quarter" class="form-control">
<option>Choose option</option>
<option>Q1</option>
<option>Q2</option>
<option>Q3</option>
<option>Q4</option>
</select>
</div>
</div>
</div>
<table class="table table-bordered">
<thead class="headings">
<tr>
<th>Code</th>
<th>Category</th>
<th>Performance Indicator</th>
<th>Response</th>
<th>Compliance</th>
<th>Notes</th>
</tr>
</thead>

<tbody>
<tr>
<td id="env1" name ="code" type="text" scope="row">ENV1</td>
<td>1.1 KPI</td>
<td>KPI Description.</td>
<td><select id="response" name="response">
<option>Yes</option>
<option>No</option>
</select>
</td>
<td><select id="Compliance" name="compliance">
<option>Compliant</option>
<option>Partially Compliant</option>
<option>Non-Compliant</option>
</select></td>
<td><input id="notes" name="notes" role="row" Type="text"></td>
</tr>
<tr>

<th id="env2" name="code" scope="row">ENV2</th>
<td>1.2 KPI</td>
<td>KPI Description.</td>
<td><select id="response" name="response">
<option>Yes</option>
<option>No</option>
</select>
</td>
<td><select id="Compliance" name="compliance">
<option>Compliant</option>
<option>Partially Compliant</option>
<option>Non-Compliant</option>
</select></td>
<td><input id="notes" name="notes" role="row" Type="text"></td>
</tr>
</tbody>
</table>


My php code to write the info to the db:



foreach($_POST['response'] as $index => $val)
{
$response = $val;
$rfname = $_POST['name'][$index];
$HospitalName = $_POST['email'][$index];
$quarter = $_POST['quarter'][$index];
$kpicode = $_POST['code'][$index];
$compliance = $_POST['compliance'][$index];
$notes = $_POST['notes'][$index];

$sql = "INSERT INTO InfoForm (Name, Email, Quarter, KPICode, Response,
Compliance, Notes)
VALUES ('$name', '$email', '$quarter', '$kpicode', '$response',
'$compliance', '$notes')";
$result = mysqli_query($conn, $sql);
}






php mysql forms loops css-tables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 22:22







PoolHall Junkie

















asked Nov 20 '18 at 21:55









PoolHall JunkiePoolHall Junkie

12




12













  • Your HTML as posted doesn't have the response, code, compliance or notes fields. Can you share more of the HTML? Without the full context we can't really answer the question.

    – cincodenada
    Nov 20 '18 at 22:02






  • 1





    Output the POST (print_r or var_dump) and see what you are actually working with. You are open to SQL injections as well

    – user3783243
    Nov 20 '18 at 22:03













  • html code updated with missing fields

    – PoolHall Junkie
    Nov 20 '18 at 22:23



















  • Your HTML as posted doesn't have the response, code, compliance or notes fields. Can you share more of the HTML? Without the full context we can't really answer the question.

    – cincodenada
    Nov 20 '18 at 22:02






  • 1





    Output the POST (print_r or var_dump) and see what you are actually working with. You are open to SQL injections as well

    – user3783243
    Nov 20 '18 at 22:03













  • html code updated with missing fields

    – PoolHall Junkie
    Nov 20 '18 at 22:23

















Your HTML as posted doesn't have the response, code, compliance or notes fields. Can you share more of the HTML? Without the full context we can't really answer the question.

– cincodenada
Nov 20 '18 at 22:02





Your HTML as posted doesn't have the response, code, compliance or notes fields. Can you share more of the HTML? Without the full context we can't really answer the question.

– cincodenada
Nov 20 '18 at 22:02




1




1





Output the POST (print_r or var_dump) and see what you are actually working with. You are open to SQL injections as well

– user3783243
Nov 20 '18 at 22:03







Output the POST (print_r or var_dump) and see what you are actually working with. You are open to SQL injections as well

– user3783243
Nov 20 '18 at 22:03















html code updated with missing fields

– PoolHall Junkie
Nov 20 '18 at 22:23





html code updated with missing fields

– PoolHall Junkie
Nov 20 '18 at 22:23












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402193%2fphp-loop-through-form-table-and-submit-data-to-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402193%2fphp-loop-through-form-table-and-submit-data-to-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]