Skip to content
PHP Logo

How can PHP handle multiple checkbox selections and transfer the data to another table effectively?

To handle multiple checkbox selections in PHP and transfer the data to another table effectively, you can use an array to store the selected values from the checkboxes. Then, you can loop through this array and insert each selected value into the desired table in your database.

php
// Assuming you have a form with checkboxes named 'checkbox[]' and a submit button

if(isset($_POST['submit'])) {
    $selectedValues = $_POST['checkbox'];
    
    // Connect to your database
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Insert each selected value into the desired table
    foreach($selectedValues as $value) {
        $sql = "INSERT INTO table_name (column_name) VALUES ('$value')";
        $conn->query($sql);
    }

    // Close the connection
    $conn->close();
}