Using a mish mash of tutorials, I have an almost 90% working solution.
Problem: When the checkboxes are selected, all records (unchecked) ones are also inserted.
My HTML file has a drop down value and a checkbox:
<input type="checkbox" name="checkbox[<?php echo $row['user_id']; ?>]">
PHP file:
$checkbox = $_POST['checkbox']
$class_id = trim($_POST['class_id']);
foreach ($checkbox as $key => $value) {
echo "{$key} => {$value} ";
//print_r($checkbox);
$sql = "INSERT INTO Class_List (user_id,class_id) VALUES (?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
mysqli_stmt_bind_param($stmt, "ii", $value, $class_id);
if(mysqli_stmt_execute($stmt)){
echo "";
} else{
echo "ERROR in inserting records: $sql. " . mysqli_error($link);
When I print the checbox Array I get:
6 => on Array ( [6] => on [7] => on )
Array
(
[6] => on
[7] => on
)
7 => on Array ( [6] => on [7] => on )
Array
(
[6] => on
[7] => on
)
The selected students have been added
I have not worked with Arrays so not sure what this output means.
Any helpp will be greatly appreciated,