Question: Whenever i submit the checkbox when pressing the Save All button, it does not update to the database. How do I debug it ? save.php

Whenever i submit the checkbox when pressing the "Save All" button, it does not update to the database. How do I debug it? save.php is in charge of handling submitting the data.
1st image: Checkbox code
2nd image: Submitform function
3rd image: Save all button
4th: Home page
save.php:
session_start();
include("./database/connection.php");
// Check if the request is a POST request with JSON data
if ($_SERVER['REQUEST_METHOD']=== 'POST' && strpos($_SERVER['CONTENT_TYPE'], 'application/json')!== false){
// Get the JSON input
$json = file_get_contents('php://input');
$data = json_decode($json, true);
// Process the data here
if ($data){
// Process the sizes
if (isset($data['size']) && (($_SESSION['level']??'')=='1')){
foreach ($data['size'] as $id => $sizes){
$sizesString = implode(',', $sizes);
// Use upsert to insert or update sizes
$stmt = $conn->prepare("INSERT INTO testdatabase3(id, size) VALUES (?,?) ON DUPLICATE KEY UPDATE size = VALUES(size)");
if ($stmt){
$stmt->bind_param('is', $id, $sizesString);
if (!$stmt->execute()){
echo json_encode(['status'=> 'error', 'message' => "Error executing statement: ". $stmt->error]);
exit();
}
$stmt->close();
} else {
echo json_encode(['status'=> 'error', 'message' => "Error preparing statement: ". $conn->error]);
exit();
}
}
// Process sizes that might have been unticked
$existingSizesQuery = "SELECT id, size FROM testdatabase3";
$result = $conn->query($existingSizesQuery);
Whenever i submit the checkbox when pressing the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!