I am trying to make a form that has several checkboxes input the value of each checked checkbox into a saeparate row on my mySQL database.
Here is my HTML:
<form action="testsent.php" method="post" novalidate>
<div id="currentWork">
<label for="currentWork">Mark all that apply.</label><br>
<input type="checkbox" name="chk1[]" value="Bernards Township">Bernards Library<br>
<input type="checkbox" name="chk1[]" value="Boonton Holmes">Boonton Library<br>
<input type="checkbox" name="chk1[]" value="Butler">Butler Library<br>
<input type="checkbox" name="chk1[]" value="Chatham">Chathams Library<br>
<input type="checkbox" name="chk1[]" value="Chester">Chester Library<br>
<input type="checkbox" name="chk1[]" value="Denville">Denville Library<br>
<input type="checkbox" name="chk1[]" value="Dover">Dover Library<br>
<input type="checkbox" name="chk1[]" value="East Hanover">East Hanover Library<br>
</div>
<input type="Submit" value="Submit" name="Submit"/>
</div>
</form>
And here is my PHP:
if(!empty($_POST['chk1'])) {
foreach($_POST['chk1'] as $check) {
//echo $check;
$sql="INSERT INTO $usertable (library) VALUES ('.$check.')";
}
}
if(mysqli_query($link, $sql))
{
echo 'Thanks for submitting';
}
else {
echo 'ERROR: Could not execute $sql.' . mysqli_error($link);
}
This is WORKING except it only inputs the final item checked. If Boonton is checked as well as Chester and Denville, it will only input 'Denville' into the form.
I thought having a foreach loop will iterate over the checkboxes, but I'm not sure where I'm going wrong. I'm not getting any error messages. I have clicked every single link that comes up when I search for this problem on stackoverflow. The problem is that there seems to be so many different ways to make this work and most of these questions are marked as having not enough information or being too generic.
If there's ANY extra info you need in order to help me resolve this, I'll be glad to provide it! I'm holding nothing back. Thanks!
<form>elementmysqli_query($link, $sql)inside the loop.