MYSQLi Multiple Checkbox Insert
I have a list of checkboxes (created via a repeat region from a MYSQLi recordset). I want the user to be able to select 1 or more of the checkboxes and then that get inserted in the database.
I have this working correctly using text boxes, however when I have used exactly the same code but with checkboxes it doesn't work. It does insert into the database however the value is blank. The HotelID value gets inserted because that is the same for all the checkboxes - but the AName value doesn't.
Strangely if I check the first checkbox in the repeat region - that value does get stored but for all of the other check boxes the value that is entered into the database is NULL. If I select other check boxes without selecting the first one in the list - they get inserted but all of them have a NULL value.
This is my code
<?php
for ($x=0; $x<sizeof($_POST['AName']); $x++) {
?>
<?php
if ((isset($_POST["Submit"]) || isset($_POST["Submit_x"]))) {
$InsertQuery = new WA_MySQLi_Query($connect_i);
$InsertQuery->Action = "insert";
$InsertQuery->Table = "amenities";
$InsertQuery->bindColumn("ResortID", "i", "".(((isset($_POST["HotelID"]))?$_POST["HotelID"]:"")) ."", "WA_DEFAULT");
$InsertQuery->bindColumn("Text", "s", "".($_POST['AName'][$x]) ."", "WA_DEFAULT");
$InsertQuery->saveInSession("");
$InsertQuery->execute();
$InsertGoTo = "index.php";
if (function_exists("rel2abs")) $InsertGoTo = $InsertGoTo?rel2abs($InsertGoTo,dirname(__FILE__)):"";
$InsertQuery->redirect($InsertGoTo);
}
?>
<?php
}
?>
<form action="" method="post" enctype="multipart/form-data" name="form1">
<div class="row">
<?php $i = 1; ?>
<?php
while(!$Recordset1->atEnd()) {
?>
<div class="col-12 col-md-6">
<input name="AName[<?php echo $i; ?>]" type="checkbox" id="AName" value="<?php echo($Recordset1->getColumnVal("AName")); ?>"> <label><?php echo($Recordset1->getColumnVal("AName")); ?> </label>
</div><?php ($i++); ?>
<?php
$Recordset1->moveNext();
}
$Recordset1->moveFirst(); //return RS to first record
?>
</div>
<input name="Submit" type="submit" id="Submit" value="Submit">
<input name="HotelID" type="hidden" id="HotelID" value="<?php echo($_GET["HotelID"]) ?>">
</form>