mercredi 20 janvier 2021

Inserting Checked Values PHP Form [closed]

So I am trying to insert the checked values (which when printing do display correct) but when suibmitting, it adds all the unchecked ones too. I am unsure what I need to do here, any ideas?

My HTML

input type="checkbox" name="checkbox[php echo $row['user_id']>]

My PHP

$checkbox = $_POST['checkbox'];
$class_id = trim($_POST['class_id']);
echo '<pre>'; print_r($checkbox); echo '</pre>';

foreach($checkbox as $value){

$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);
}
} else{
echo "ERROR: Could not insert records: $sql. " . mysqli_error($link);
}
}
// Close statement

mysqli_stmt_close($stmt);
// Close connection
mysqli_close($link);



Aucun commentaire:

Enregistrer un commentaire