How can checkbox values be pre-filled with data from a database in a PHP form?

To pre-fill checkbox values with data from a database in a PHP form, you can fetch the data from the database and compare it with the values of the checkboxes. If a checkbox value matches the data from the database, you can add the "checked" attribute to that checkbox. This way, the checkboxes will be pre-filled based on the data retrieved from the database.

// Assume $dbData is an array of values retrieved from the database
// Assume $checkboxValues is an array of checkbox values

foreach ($checkboxValues as $checkbox) {
    $checked = (in_array($checkbox, $dbData)) ? 'checked' : '';
    echo '<input type="checkbox" name="checkbox[]" value="' . $checkbox . '" ' . $checked . '> ' . $checkbox . '<br>';
}