Php not handling form data correctly

I have A court rental page where people can choose amenities for their courts, after checking checkboxes and submitting the form, Php is only receiving 1 of the checkboxes, I basically want to take the id of the court I create, and the id of the selected amenities from the database and put them in a junction table so it would look something like
Court_id Amenity_id
1 1
1 2 etc…

but after pressing submit, all the court details are getting saved correctly in the court database, but the courtamenity junction table stays empty all the time.
Here is the relevant code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <div class="amenities">
<label><input type="checkbox" name="amenities[]" value="Cafeteria"> Cafeteria</label>
<label><input type="checkbox" name="amenities[]" value="Showers"> Showers</label>
<label><input type="checkbox" name="amenities[]" value="Wifi"> Wifi</label>
<label><input type="checkbox" name="amenities[]" value="Toilets"> Toilets</label>
<label><input type="checkbox" name="amenities[]" value="Gym"> Gym</label>
<label><input type="checkbox" name="amenities[]" value="Parking"> Parking</label>
<label><input type="checkbox" name="amenities[]" value="Indoors"> Indoors</label>
<label><input type="checkbox" name="amenities[]" value="Outdoors"> Outdoors</label>
</div>
</code>
<code> <div class="amenities"> <label><input type="checkbox" name="amenities[]" value="Cafeteria"> Cafeteria</label> <label><input type="checkbox" name="amenities[]" value="Showers"> Showers</label> <label><input type="checkbox" name="amenities[]" value="Wifi"> Wifi</label> <label><input type="checkbox" name="amenities[]" value="Toilets"> Toilets</label> <label><input type="checkbox" name="amenities[]" value="Gym"> Gym</label> <label><input type="checkbox" name="amenities[]" value="Parking"> Parking</label> <label><input type="checkbox" name="amenities[]" value="Indoors"> Indoors</label> <label><input type="checkbox" name="amenities[]" value="Outdoors"> Outdoors</label> </div> </code>
 <div class="amenities">
                            <label><input type="checkbox" name="amenities[]" value="Cafeteria"> Cafeteria</label>
                            <label><input type="checkbox" name="amenities[]" value="Showers"> Showers</label>
                            <label><input type="checkbox" name="amenities[]" value="Wifi"> Wifi</label>
                            <label><input type="checkbox" name="amenities[]" value="Toilets"> Toilets</label>
                            <label><input type="checkbox" name="amenities[]" value="Gym"> Gym</label>
                            <label><input type="checkbox" name="amenities[]" value="Parking"> Parking</label>
                            <label><input type="checkbox" name="amenities[]" value="Indoors"> Indoors</label>
                            <label><input type="checkbox" name="amenities[]" value="Outdoors"> Outdoors</label>
                        </div>
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// Get the last inserted court ID
$court_id = $conn->insert_id;
// Fetch selected amenities from the form
if (isset($_POST['amenities']) && is_array($_POST['amenities'])) {
$selected_amenities = $_POST['amenities'];
} else {
$selected_amenities = array(); // No amenities selected
}
// Prepare and execute SQL query to insert amenities
if (!empty($selected_amenities)) {
echo '<pre>';
print_r($selected_amenities);
echo '</pre>';
$sql_court_amenity = "INSERT INTO courtamenity (court_id, amenity_id) VALUES (?, ?)";
$stmt_court_amenity = $conn->prepare($sql_court_amenity);
if (!$stmt_court_amenity) {
die('Error preparing statement: ' . $conn->error);
}
foreach ($selected_amenities as $amenity_name) {
// Check if the amenity exists
$sql_check = "SELECT amenity_id FROM amenity WHERE name = ?";
$stmt_check = $conn->prepare($sql_check);
if (!$stmt_check) {
die('Error preparing statement: ' . $conn->error);
}
$stmt_check->bind_param('s', $amenity_name);
$stmt_check->execute();
$result = $stmt_check->get_result();
if ($row = $result->fetch_assoc()) {
$amenity_id = $row['amenity_id'];
// Insert into courtamenity table
$stmt_court_amenity->bind_param('ii', $court_id, $amenity_id);
if (!$stmt_court_amenity->execute()) {
echo "Error inserting into courtamenity: " . $stmt_court_amenity->error;
}
} else {
echo "No matching amenity found for: " . $amenity_name;
}
$stmt_check->close();
}
$stmt_court_amenity->close();
}
</code>
<code>// Get the last inserted court ID $court_id = $conn->insert_id; // Fetch selected amenities from the form if (isset($_POST['amenities']) && is_array($_POST['amenities'])) { $selected_amenities = $_POST['amenities']; } else { $selected_amenities = array(); // No amenities selected } // Prepare and execute SQL query to insert amenities if (!empty($selected_amenities)) { echo '<pre>'; print_r($selected_amenities); echo '</pre>'; $sql_court_amenity = "INSERT INTO courtamenity (court_id, amenity_id) VALUES (?, ?)"; $stmt_court_amenity = $conn->prepare($sql_court_amenity); if (!$stmt_court_amenity) { die('Error preparing statement: ' . $conn->error); } foreach ($selected_amenities as $amenity_name) { // Check if the amenity exists $sql_check = "SELECT amenity_id FROM amenity WHERE name = ?"; $stmt_check = $conn->prepare($sql_check); if (!$stmt_check) { die('Error preparing statement: ' . $conn->error); } $stmt_check->bind_param('s', $amenity_name); $stmt_check->execute(); $result = $stmt_check->get_result(); if ($row = $result->fetch_assoc()) { $amenity_id = $row['amenity_id']; // Insert into courtamenity table $stmt_court_amenity->bind_param('ii', $court_id, $amenity_id); if (!$stmt_court_amenity->execute()) { echo "Error inserting into courtamenity: " . $stmt_court_amenity->error; } } else { echo "No matching amenity found for: " . $amenity_name; } $stmt_check->close(); } $stmt_court_amenity->close(); } </code>
// Get the last inserted court ID
$court_id = $conn->insert_id;

// Fetch selected amenities from the form
if (isset($_POST['amenities']) && is_array($_POST['amenities'])) {
    $selected_amenities = $_POST['amenities'];
} else {
    $selected_amenities = array(); // No amenities selected
}
// Prepare and execute SQL query to insert amenities
if (!empty($selected_amenities)) {

    echo '<pre>';
    print_r($selected_amenities);
    echo '</pre>';


    $sql_court_amenity = "INSERT INTO courtamenity (court_id, amenity_id) VALUES (?, ?)";
    $stmt_court_amenity = $conn->prepare($sql_court_amenity);
    
    if (!$stmt_court_amenity) {
        die('Error preparing statement: ' . $conn->error);
    }

    foreach ($selected_amenities as $amenity_name) {
        // Check if the amenity exists
        $sql_check = "SELECT amenity_id FROM amenity WHERE name = ?";
        $stmt_check = $conn->prepare($sql_check);

        if (!$stmt_check) {
            die('Error preparing statement: ' . $conn->error);
        }

        $stmt_check->bind_param('s', $amenity_name);
        $stmt_check->execute();
        $result = $stmt_check->get_result();

        if ($row = $result->fetch_assoc()) {
            $amenity_id = $row['amenity_id'];
            
            // Insert into courtamenity table
            $stmt_court_amenity->bind_param('ii', $court_id, $amenity_id);
            if (!$stmt_court_amenity->execute()) {
                echo "Error inserting into courtamenity: " . $stmt_court_amenity->error;
            }
        } else {
            echo "No matching amenity found for: " . $amenity_name;
        }

        $stmt_check->close();
    }
    
    $stmt_court_amenity->close();
}

I tried debugging the $_Post using

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>echo '<pre>';
print_r($_POST);
echo '</pre>';
</code>
<code>echo '<pre>'; print_r($_POST); echo '</pre>'; </code>
echo '<pre>';
print_r($_POST);
echo '</pre>';

and it shows only 1 amenity, but in the f12 console log, in the network section it is showing in the form-data that all 3 amenities are being sent

Recognized by PHP Collective

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật