So for context I currently have an HTML form page where I allow the user to create a new book entry. As part of the form the user needs to choose between 3 different checkboxes (Horror, Fantasy, and Mystery). Will add more options later down the line.
The problem I am having is that when I’m trying to save which option are chosen from the checkboxes into the same SQL entry. But all the examples I’ve found for checkboxes want to create a new SQL entry for each option chosen. It seems to want to create the new entry for each option chosen even if every other variable in the entry stays the same. An example would be the following stack overflow post from a few years ago where (At least to my understanding of the post/code) implies that in the controller script I need to go through each value in the array and create a new SQL entry for it. I would rather not do this as it will mass populate the table.
Saving the selected rows by checkbox to database in php
I am currently using Codeigniter 4, HTML/CSS, and PHP to make the website. I have the code for the HTML form below. The PHP controller is set up to receive all the other information from the form. I am only missing the info from BookType[]
<form method="post" id="add_create" name="add_create" action="<?= '/public/book-submit2' ?>">
<!-- Other form inputs -->
<div class="col-md-4">
<div class="form-group">
<label class="text-secondary" for="BookType"><em>*Type Of book:</em></label>
<p>
<form required>
<input type="checkbox" id="Horror" name="BookType[]" value="Horror">
<label for="Horror"> Horror</label><br>
<input type="checkbox" id="Fantasy" name="BookType[]" value="Fantasy">
<label for="Fantasy"> Fantasy </label><br>
<input type="checkbox" id="Mystery" name="BookType[]" value="Mystery">
<label for="Mystery"> Mystery </label><br>
</form>
</div><!-- /.form-group -->
</div><!-- /.col -->
</form>
Is there something I am not realizing with the PHP code or SQL? I’ve only been using SQL and CodeIgnitor for a few months.