PHP Form array has empty values when inserting into MySQL table












1














I'm having an issue inserting php form array values into a MySQL table. Right now the form itself pulls worker information from another table to populate the fields and their values. Right now there are only three workers that would populate those fields, however as more workers get added, there could be as many as 40. When I add just the first worker from the form, all the information inserts normally. However, when I add more than one, the title and employeeId fields are blank and I can't figure out why. Any help would be greatly appreciated.



Here is the form:



<form method = 'POST' action = 'addworkers.php'>
<?php
$sql2 = "select * from workers where companyId = 1";
$result2 = mysqli_query($conn,$sql2);
$numRows = mysqli_num_rows($result2);
$check = 0;
while($row2 = $result2->fetch_assoc()) {
$employeeId = $row2["id"];
$name = $row2["name"];
echo '<input type="checkbox" name="employeeId" value="' . $employeeId . '">';
echo "$namen";
echo '<input type = "hidden" value="' . $companyId . '" name = "companyId"/>';
echo '<input type = "hidden" value="' . $jobNumber . '" name = "jobNumber"/>';
echo 'Site Title : <input type = "text" name = "title"/><br/>';
}
?>
<input type="hidden" name="count" value="<?php echo "$numRows"; ?>"/>
<input type = 'submit' value = 'SEND'/>
</form>


Then the addworkers.php code



require_once("dbConfig.php");
session_start();
$timestamp = date("Y-m-d");

if (isset($_SESSION['loginname'])) {
$companyId = isset($_POST['companyId']) ? $_POST['companyId'] : "" ;
$jobNumber = isset($_POST['jobNumber']) ? $_POST['jobNumber'] : "" ;
$employeeId = isset($_POST['employeeId']) ? $_POST['employeeId'] : "" ;
$title = isset($_POST['title']) ? $_POST['title'] : "" ;

foreach($title as $key=>$value){
if (!empty($value)) {
$query = "insert into `Jobs` (id, companyId, jobId, employeeId, siteTitle, dateAdded) values (NULL,'$companyId[$key]', '$jobNumber[$key]','$employeeId[$key]','$value','$timestamp')";
$result = mysqli_query($conn,$query);
}
}
} else {
echo "Error";
}


I changed the echo "Error" line but the output was clear.



It must be a problem with how the array is counted but I'm not sure how to fix it. in the form, if I check the boxes next to each row, all the information is entered into the table properly. If I only check the second and/or third line, it doesn't include the Site Title and the employeeId is reversed.



Here is the output of the form:



<form method = 'POST' action = 'insertworkers.php'>
<input type="checkbox" name="employeeId" value="1">Mike
<input type = "hidden" value="1" name = "companyId"/>
<input type = "hidden" value="12345" name = "jobNumber"/>
Site Title : <input type = "text" name = "title"/>

<input type="checkbox" name="employeeId" value="2">Steve
<input type = "hidden" value="1" name = "companyId"/>
<input type = "hidden" value="12345" name = "jobNumber"/>
Site Title : <input type = "text" name = "title"/>

<input type="checkbox" name="employeeId" value="3">Roger
<input type = "hidden" value="1" name = "companyId"/>
<input type = "hidden" value="12345" name = "jobNumber"/>
Site Title : <input type = "text" name = "title"/>

<input type="hidden" name="count" value="3"/>
<input type = 'submit' value = 'SEND'/>
</form>


I also changed the foreach loop to a for loop since the array depth should be the same as all fields will be mandatory



require_once("dbConfig.php");
session_start();
$counter = "".$_POST["count"]."";
$timestamp = date("Y-m-d");

if ( isset( $_SESSION['loginname'] ) ) {
$companyId = isset($_POST['companyId']) ? $_POST['companyId'] : "" ;
$jobNumber = isset($_POST['jobNumber']) ? $_POST['jobNumber'] : "" ;
$employeeId = isset($_POST['employeeId']) ? $_POST['employeeId'] : "" ;
$title = isset($_POST['title']) ? $_POST['title'] : "" ;

for($i=0, $count = count($employeeId);$i<$count;$i++){
if (!empty($employeeId)) {
$query = "insert into `customerJobs` (id, companyId, jobId, employeeId, siteTitle, dateAdded) values (NULL,'$companyId', '$jobNumber','$employeeId[$i]','$title[$i]','$timestamp')";
$result = mysqli_query($conn,$query);
}
}
} else {
echo "Error";
}









share|improve this question
























  • Change your echo "Error"; to echo "Error" . mysqli_error($conn); - is there anything?
    – Funk Forty Niner
    Nov 21 at 3:03










  • also use error reporting php.net/manual/en/function.error-reporting.php
    – Funk Forty Niner
    Nov 21 at 3:06










  • It would be more helpful to include the actual HTML of the form, not the PHP that generated it.
    – miken32
    Nov 21 at 3:07










  • open to SQL injection attack, dont use this in actual production
    – user10226920
    Nov 21 at 3:10










  • You're only doing a foreach for the one array here, what about the others?
    – Funk Forty Niner
    Nov 21 at 3:11
















1














I'm having an issue inserting php form array values into a MySQL table. Right now the form itself pulls worker information from another table to populate the fields and their values. Right now there are only three workers that would populate those fields, however as more workers get added, there could be as many as 40. When I add just the first worker from the form, all the information inserts normally. However, when I add more than one, the title and employeeId fields are blank and I can't figure out why. Any help would be greatly appreciated.



Here is the form:



<form method = 'POST' action = 'addworkers.php'>
<?php
$sql2 = "select * from workers where companyId = 1";
$result2 = mysqli_query($conn,$sql2);
$numRows = mysqli_num_rows($result2);
$check = 0;
while($row2 = $result2->fetch_assoc()) {
$employeeId = $row2["id"];
$name = $row2["name"];
echo '<input type="checkbox" name="employeeId" value="' . $employeeId . '">';
echo "$namen";
echo '<input type = "hidden" value="' . $companyId . '" name = "companyId"/>';
echo '<input type = "hidden" value="' . $jobNumber . '" name = "jobNumber"/>';
echo 'Site Title : <input type = "text" name = "title"/><br/>';
}
?>
<input type="hidden" name="count" value="<?php echo "$numRows"; ?>"/>
<input type = 'submit' value = 'SEND'/>
</form>


Then the addworkers.php code



require_once("dbConfig.php");
session_start();
$timestamp = date("Y-m-d");

if (isset($_SESSION['loginname'])) {
$companyId = isset($_POST['companyId']) ? $_POST['companyId'] : "" ;
$jobNumber = isset($_POST['jobNumber']) ? $_POST['jobNumber'] : "" ;
$employeeId = isset($_POST['employeeId']) ? $_POST['employeeId'] : "" ;
$title = isset($_POST['title']) ? $_POST['title'] : "" ;

foreach($title as $key=>$value){
if (!empty($value)) {
$query = "insert into `Jobs` (id, companyId, jobId, employeeId, siteTitle, dateAdded) values (NULL,'$companyId[$key]', '$jobNumber[$key]','$employeeId[$key]','$value','$timestamp')";
$result = mysqli_query($conn,$query);
}
}
} else {
echo "Error";
}


I changed the echo "Error" line but the output was clear.



It must be a problem with how the array is counted but I'm not sure how to fix it. in the form, if I check the boxes next to each row, all the information is entered into the table properly. If I only check the second and/or third line, it doesn't include the Site Title and the employeeId is reversed.



Here is the output of the form:



<form method = 'POST' action = 'insertworkers.php'>
<input type="checkbox" name="employeeId" value="1">Mike
<input type = "hidden" value="1" name = "companyId"/>
<input type = "hidden" value="12345" name = "jobNumber"/>
Site Title : <input type = "text" name = "title"/>

<input type="checkbox" name="employeeId" value="2">Steve
<input type = "hidden" value="1" name = "companyId"/>
<input type = "hidden" value="12345" name = "jobNumber"/>
Site Title : <input type = "text" name = "title"/>

<input type="checkbox" name="employeeId" value="3">Roger
<input type = "hidden" value="1" name = "companyId"/>
<input type = "hidden" value="12345" name = "jobNumber"/>
Site Title : <input type = "text" name = "title"/>

<input type="hidden" name="count" value="3"/>
<input type = 'submit' value = 'SEND'/>
</form>


I also changed the foreach loop to a for loop since the array depth should be the same as all fields will be mandatory



require_once("dbConfig.php");
session_start();
$counter = "".$_POST["count"]."";
$timestamp = date("Y-m-d");

if ( isset( $_SESSION['loginname'] ) ) {
$companyId = isset($_POST['companyId']) ? $_POST['companyId'] : "" ;
$jobNumber = isset($_POST['jobNumber']) ? $_POST['jobNumber'] : "" ;
$employeeId = isset($_POST['employeeId']) ? $_POST['employeeId'] : "" ;
$title = isset($_POST['title']) ? $_POST['title'] : "" ;

for($i=0, $count = count($employeeId);$i<$count;$i++){
if (!empty($employeeId)) {
$query = "insert into `customerJobs` (id, companyId, jobId, employeeId, siteTitle, dateAdded) values (NULL,'$companyId', '$jobNumber','$employeeId[$i]','$title[$i]','$timestamp')";
$result = mysqli_query($conn,$query);
}
}
} else {
echo "Error";
}









share|improve this question
























  • Change your echo "Error"; to echo "Error" . mysqli_error($conn); - is there anything?
    – Funk Forty Niner
    Nov 21 at 3:03










  • also use error reporting php.net/manual/en/function.error-reporting.php
    – Funk Forty Niner
    Nov 21 at 3:06










  • It would be more helpful to include the actual HTML of the form, not the PHP that generated it.
    – miken32
    Nov 21 at 3:07










  • open to SQL injection attack, dont use this in actual production
    – user10226920
    Nov 21 at 3:10










  • You're only doing a foreach for the one array here, what about the others?
    – Funk Forty Niner
    Nov 21 at 3:11














1












1








1







I'm having an issue inserting php form array values into a MySQL table. Right now the form itself pulls worker information from another table to populate the fields and their values. Right now there are only three workers that would populate those fields, however as more workers get added, there could be as many as 40. When I add just the first worker from the form, all the information inserts normally. However, when I add more than one, the title and employeeId fields are blank and I can't figure out why. Any help would be greatly appreciated.



Here is the form:



<form method = 'POST' action = 'addworkers.php'>
<?php
$sql2 = "select * from workers where companyId = 1";
$result2 = mysqli_query($conn,$sql2);
$numRows = mysqli_num_rows($result2);
$check = 0;
while($row2 = $result2->fetch_assoc()) {
$employeeId = $row2["id"];
$name = $row2["name"];
echo '<input type="checkbox" name="employeeId" value="' . $employeeId . '">';
echo "$namen";
echo '<input type = "hidden" value="' . $companyId . '" name = "companyId"/>';
echo '<input type = "hidden" value="' . $jobNumber . '" name = "jobNumber"/>';
echo 'Site Title : <input type = "text" name = "title"/><br/>';
}
?>
<input type="hidden" name="count" value="<?php echo "$numRows"; ?>"/>
<input type = 'submit' value = 'SEND'/>
</form>


Then the addworkers.php code



require_once("dbConfig.php");
session_start();
$timestamp = date("Y-m-d");

if (isset($_SESSION['loginname'])) {
$companyId = isset($_POST['companyId']) ? $_POST['companyId'] : "" ;
$jobNumber = isset($_POST['jobNumber']) ? $_POST['jobNumber'] : "" ;
$employeeId = isset($_POST['employeeId']) ? $_POST['employeeId'] : "" ;
$title = isset($_POST['title']) ? $_POST['title'] : "" ;

foreach($title as $key=>$value){
if (!empty($value)) {
$query = "insert into `Jobs` (id, companyId, jobId, employeeId, siteTitle, dateAdded) values (NULL,'$companyId[$key]', '$jobNumber[$key]','$employeeId[$key]','$value','$timestamp')";
$result = mysqli_query($conn,$query);
}
}
} else {
echo "Error";
}


I changed the echo "Error" line but the output was clear.



It must be a problem with how the array is counted but I'm not sure how to fix it. in the form, if I check the boxes next to each row, all the information is entered into the table properly. If I only check the second and/or third line, it doesn't include the Site Title and the employeeId is reversed.



Here is the output of the form:



<form method = 'POST' action = 'insertworkers.php'>
<input type="checkbox" name="employeeId" value="1">Mike
<input type = "hidden" value="1" name = "companyId"/>
<input type = "hidden" value="12345" name = "jobNumber"/>
Site Title : <input type = "text" name = "title"/>

<input type="checkbox" name="employeeId" value="2">Steve
<input type = "hidden" value="1" name = "companyId"/>
<input type = "hidden" value="12345" name = "jobNumber"/>
Site Title : <input type = "text" name = "title"/>

<input type="checkbox" name="employeeId" value="3">Roger
<input type = "hidden" value="1" name = "companyId"/>
<input type = "hidden" value="12345" name = "jobNumber"/>
Site Title : <input type = "text" name = "title"/>

<input type="hidden" name="count" value="3"/>
<input type = 'submit' value = 'SEND'/>
</form>


I also changed the foreach loop to a for loop since the array depth should be the same as all fields will be mandatory



require_once("dbConfig.php");
session_start();
$counter = "".$_POST["count"]."";
$timestamp = date("Y-m-d");

if ( isset( $_SESSION['loginname'] ) ) {
$companyId = isset($_POST['companyId']) ? $_POST['companyId'] : "" ;
$jobNumber = isset($_POST['jobNumber']) ? $_POST['jobNumber'] : "" ;
$employeeId = isset($_POST['employeeId']) ? $_POST['employeeId'] : "" ;
$title = isset($_POST['title']) ? $_POST['title'] : "" ;

for($i=0, $count = count($employeeId);$i<$count;$i++){
if (!empty($employeeId)) {
$query = "insert into `customerJobs` (id, companyId, jobId, employeeId, siteTitle, dateAdded) values (NULL,'$companyId', '$jobNumber','$employeeId[$i]','$title[$i]','$timestamp')";
$result = mysqli_query($conn,$query);
}
}
} else {
echo "Error";
}









share|improve this question















I'm having an issue inserting php form array values into a MySQL table. Right now the form itself pulls worker information from another table to populate the fields and their values. Right now there are only three workers that would populate those fields, however as more workers get added, there could be as many as 40. When I add just the first worker from the form, all the information inserts normally. However, when I add more than one, the title and employeeId fields are blank and I can't figure out why. Any help would be greatly appreciated.



Here is the form:



<form method = 'POST' action = 'addworkers.php'>
<?php
$sql2 = "select * from workers where companyId = 1";
$result2 = mysqli_query($conn,$sql2);
$numRows = mysqli_num_rows($result2);
$check = 0;
while($row2 = $result2->fetch_assoc()) {
$employeeId = $row2["id"];
$name = $row2["name"];
echo '<input type="checkbox" name="employeeId" value="' . $employeeId . '">';
echo "$namen";
echo '<input type = "hidden" value="' . $companyId . '" name = "companyId"/>';
echo '<input type = "hidden" value="' . $jobNumber . '" name = "jobNumber"/>';
echo 'Site Title : <input type = "text" name = "title"/><br/>';
}
?>
<input type="hidden" name="count" value="<?php echo "$numRows"; ?>"/>
<input type = 'submit' value = 'SEND'/>
</form>


Then the addworkers.php code



require_once("dbConfig.php");
session_start();
$timestamp = date("Y-m-d");

if (isset($_SESSION['loginname'])) {
$companyId = isset($_POST['companyId']) ? $_POST['companyId'] : "" ;
$jobNumber = isset($_POST['jobNumber']) ? $_POST['jobNumber'] : "" ;
$employeeId = isset($_POST['employeeId']) ? $_POST['employeeId'] : "" ;
$title = isset($_POST['title']) ? $_POST['title'] : "" ;

foreach($title as $key=>$value){
if (!empty($value)) {
$query = "insert into `Jobs` (id, companyId, jobId, employeeId, siteTitle, dateAdded) values (NULL,'$companyId[$key]', '$jobNumber[$key]','$employeeId[$key]','$value','$timestamp')";
$result = mysqli_query($conn,$query);
}
}
} else {
echo "Error";
}


I changed the echo "Error" line but the output was clear.



It must be a problem with how the array is counted but I'm not sure how to fix it. in the form, if I check the boxes next to each row, all the information is entered into the table properly. If I only check the second and/or third line, it doesn't include the Site Title and the employeeId is reversed.



Here is the output of the form:



<form method = 'POST' action = 'insertworkers.php'>
<input type="checkbox" name="employeeId" value="1">Mike
<input type = "hidden" value="1" name = "companyId"/>
<input type = "hidden" value="12345" name = "jobNumber"/>
Site Title : <input type = "text" name = "title"/>

<input type="checkbox" name="employeeId" value="2">Steve
<input type = "hidden" value="1" name = "companyId"/>
<input type = "hidden" value="12345" name = "jobNumber"/>
Site Title : <input type = "text" name = "title"/>

<input type="checkbox" name="employeeId" value="3">Roger
<input type = "hidden" value="1" name = "companyId"/>
<input type = "hidden" value="12345" name = "jobNumber"/>
Site Title : <input type = "text" name = "title"/>

<input type="hidden" name="count" value="3"/>
<input type = 'submit' value = 'SEND'/>
</form>


I also changed the foreach loop to a for loop since the array depth should be the same as all fields will be mandatory



require_once("dbConfig.php");
session_start();
$counter = "".$_POST["count"]."";
$timestamp = date("Y-m-d");

if ( isset( $_SESSION['loginname'] ) ) {
$companyId = isset($_POST['companyId']) ? $_POST['companyId'] : "" ;
$jobNumber = isset($_POST['jobNumber']) ? $_POST['jobNumber'] : "" ;
$employeeId = isset($_POST['employeeId']) ? $_POST['employeeId'] : "" ;
$title = isset($_POST['title']) ? $_POST['title'] : "" ;

for($i=0, $count = count($employeeId);$i<$count;$i++){
if (!empty($employeeId)) {
$query = "insert into `customerJobs` (id, companyId, jobId, employeeId, siteTitle, dateAdded) values (NULL,'$companyId', '$jobNumber','$employeeId[$i]','$title[$i]','$timestamp')";
$result = mysqli_query($conn,$query);
}
}
} else {
echo "Error";
}






php mysql arrays forms session






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 10:24









DestinatioN

1,2481326




1,2481326










asked Nov 21 at 3:01









Matt

192




192












  • Change your echo "Error"; to echo "Error" . mysqli_error($conn); - is there anything?
    – Funk Forty Niner
    Nov 21 at 3:03










  • also use error reporting php.net/manual/en/function.error-reporting.php
    – Funk Forty Niner
    Nov 21 at 3:06










  • It would be more helpful to include the actual HTML of the form, not the PHP that generated it.
    – miken32
    Nov 21 at 3:07










  • open to SQL injection attack, dont use this in actual production
    – user10226920
    Nov 21 at 3:10










  • You're only doing a foreach for the one array here, what about the others?
    – Funk Forty Niner
    Nov 21 at 3:11


















  • Change your echo "Error"; to echo "Error" . mysqli_error($conn); - is there anything?
    – Funk Forty Niner
    Nov 21 at 3:03










  • also use error reporting php.net/manual/en/function.error-reporting.php
    – Funk Forty Niner
    Nov 21 at 3:06










  • It would be more helpful to include the actual HTML of the form, not the PHP that generated it.
    – miken32
    Nov 21 at 3:07










  • open to SQL injection attack, dont use this in actual production
    – user10226920
    Nov 21 at 3:10










  • You're only doing a foreach for the one array here, what about the others?
    – Funk Forty Niner
    Nov 21 at 3:11
















Change your echo "Error"; to echo "Error" . mysqli_error($conn); - is there anything?
– Funk Forty Niner
Nov 21 at 3:03




Change your echo "Error"; to echo "Error" . mysqli_error($conn); - is there anything?
– Funk Forty Niner
Nov 21 at 3:03












also use error reporting php.net/manual/en/function.error-reporting.php
– Funk Forty Niner
Nov 21 at 3:06




also use error reporting php.net/manual/en/function.error-reporting.php
– Funk Forty Niner
Nov 21 at 3:06












It would be more helpful to include the actual HTML of the form, not the PHP that generated it.
– miken32
Nov 21 at 3:07




It would be more helpful to include the actual HTML of the form, not the PHP that generated it.
– miken32
Nov 21 at 3:07












open to SQL injection attack, dont use this in actual production
– user10226920
Nov 21 at 3:10




open to SQL injection attack, dont use this in actual production
– user10226920
Nov 21 at 3:10












You're only doing a foreach for the one array here, what about the others?
– Funk Forty Niner
Nov 21 at 3:11




You're only doing a foreach for the one array here, what about the others?
– Funk Forty Niner
Nov 21 at 3:11

















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%2f53404688%2fphp-form-array-has-empty-values-when-inserting-into-mysql-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53404688%2fphp-form-array-has-empty-values-when-inserting-into-mysql-table%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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'