<?php session_start(); ?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "membership";

$connection = new mysqli($servername, $username, $password, $database);
$id = "";
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>

    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card mt-5">
                    <div class="card-header">
                        <h4>Tambahn Peerta</h4>
                    </div>
                    <div class="card-body">

                        <form method="POST">
                            <?php
                            $getnama = "SELECT * FROM peserta";
                            $dftrpeserta = mysqli_query($connection, $getnama);

                            if (mysqli_num_rows($dftrpeserta) > 0) {
                                foreach ($dftrpeserta as $listpeserta) {
                                    ?>
                                    <input type="checkbox" name="pesertaslist[]" value="<?= $listpeserta['nama_peserta']; ?>" />
                                    <?= $listpeserta['nama_peserta']; ?> <br />
                                    <?php
                                }
                            } else {
                                echo "No Record Found";
                            }
                            ?>
                            <div class="form-group mt-3">
                                <button name="save_multicheckbox" class="btn btn-primary">Simpan</button>
                            </div>
                            <?php
                            if (isset($_POST['save_multicheckbox'])) {

                                $id = $_GET["id"];
                                $pesertalist = $_POST['pesertaslist'];
                                $xyz = implode(", ", $pesertalist);
                                $query = "UPDATE lomba set peserta='$xyz' where id='$id'";
                                $konek = mysqli_query($connection, $query);

                                if ($konek) {
                                    header("Location: datalombacch.php");
                                }

                            }
                            ?>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>