How do I insert multiple checkbox values into a table?

How do I insert multiple checkbox values into a table?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Handling multiple checkbox inputs in a web form often trips up developers because of how HTML and PHP handle arrays by default. The core issue isn't the database or the loop logic; it is the name attribute assigned to your input fields. In your specific example, every single checkbox shares the same name: Days.

When a browser submits a form with multiple checkboxes selected, it expects the server to receive an array of values for that key. However, if you look closely at the HTML provided, there is a subtle but critical detail missing from standard practice: the brackets in the name attribute. While some browsers are lenient and will automatically wrap the submitted values in an array if multiple inputs share a name, relying on this behavior is risky and inconsistent across different client environments.

To ensure your PHP script receives an indexed array like ['Daily', 'Sunday'], you must explicitly tell the browser to treat it as such. Update your HTML form fields to use name="Days[]":

<input type="checkbox" name="Days[]" value="Daily">Daily
<input type="checkbox" name="Days[]" value="Sunday">Sunday

Once this is done, when you access $_POST['Days'] in PHP, it will reliably return an array. If no checkboxes are selected, it might be empty or undefined depending on the framework, which is why your existing if(isset($_POST['submit'])) check is actually a good practice here; it ensures you only run the insertion logic when the user has actively clicked the submit button.

Your current PHP logic attempts to insert each value into a column named orange. This suggests your database table expects a single string per row, but you are trying to store multiple days of flight availability in separate rows under that same name. If your intention is to record which days were selected for a specific booking or query, this approach creates a fragmented dataset. For instance, if "Daily" and "Monday" are selected, your database will contain two rows: one with orange='Daily' and another with orange='Monday'. This makes querying complex; finding all records for a specific date requires multiple joins or subqueries.

A more robust architectural pattern is to treat the selection of checkboxes as a set belonging to a single record. Instead of creating new rows in a table called example, you should insert one row into your main table (let's say flights) containing the booking ID, and then store the selected days as a serialized string or a JSON blob in a dedicated column, such as selected_days.

Alternatively, if you need to query these values individually later, consider using a junction table. You would create a table like flight_schedule with foreign keys to your main flight record and a column for the day name. This allows you to easily filter by day without complex string parsing.

It is also worth addressing the database connection code used in your source material. The mysql_* functions are deprecated and removed in PHP 7.0+. You should migrate this logic to mysqli or PDO immediately to avoid security vulnerabilities and compatibility issues. Furthermore, concatenating variables directly into SQL queries creates a massive vulnerability known as SQL Injection. Even though your current code breaks on the first error with or die, a malicious actor could craft an input string that alters your query logic. Always use prepared statements:

foreach ($checkBox as $day) {
    $stmt = $mysqli->prepare("INSERT INTO example (orange) VALUES (?)");
    $stmt->bind_param("s", $day);
    $stmt->execute();
}

If you are working within the Laravel ecosystem, this manual loop and database interaction would be abstracted away using Eloquent or Query Builder. Laravel handles array inputs from checkboxes automatically if your migration defines the column correctly, often utilizing json columns for storing collections of values like days, which simplifies the relationship management significantly.

The most common pitfall here is failing to validate the input before processing. Ensure that $checkBox contains only valid day names. If a user submits the form and selects nothing, $_POST['Days'] will be empty, and your loop simply won't execute, preventing any erroneous inserts. Always check for an empty array before iterating.

Our network