csrtechnew.ohctech.in/save_parents_meeting.php
2025-08-29 16:30:39 +05:30

98 lines
3.8 KiB
PHP

<?php
include('includes/config/config.php');
include('includes/functions.php');
include('log_entry.php');
// Logging request parameters
$requestStr = "";
foreach ($_REQUEST as $key => $value) {
$requestStr .= $key . " : " . $value . "\n";
error_log($key . " : " . $value . "\n");
}
// Assuming session is started elsewhere
$modified_by = $_SESSION['user_id'] ?? null;
if (!$modified_by) {
header('HTTP/1.1 400 Bad Request');
die(json_encode(['error' => 'User not authenticated']));
}
// Extract POST data
$noOfRows = (int) $_POST['count_items'];
$ppm_id = $_POST['ppm_id'];
$batch_name = $_POST['batch_name'];
$teacher_name = $_POST['teacher_name'];
$meeting_date = $_POST['meeting_date'];
$teacher_ids = $_REQUEST['teacher_name'];
$teachers = implode(',', $teacher_ids);
$ohc_type_id = $_SESSION['current_ohcttype'];
// Start transaction
mysqli_begin_transaction($conn);
try {
if ($ppm_id > 0) {
// Update existing records
$query = "UPDATE primary_parents_meeting
SET batch_name = '$batch_name', meeting_date = '$meeting_date', teacher_name = '$teachers',ohc_type_id='$ohc_type_id', modified_by = '$modified_by'
WHERE ppm_id = '$ppm_id'";
if (!mysqli_query($conn, $query)) {
throw new Exception("Failed to update primary_parents_meeting: " . mysqli_error($conn));
}
} else {
// Insert new records
$query = "INSERT INTO primary_parents_meeting (batch_name, meeting_date, teacher_name,ohc_type_id, modified_by)
VALUES ('$batch_name', '$meeting_date', '$teachers','$ohc_type_id', '$modified_by')";
if (!mysqli_query($conn, $query)) {
throw new Exception("Failed to insert into primary_parents_meeting: " . mysqli_error($conn));
}
$ppm_id = mysqli_insert_id($conn); // Get the auto-generated ID from the last INSERT query
}
// Process child_parents_meeting records
for ($i = 0; $i < $noOfRows; $i++) {
$cpmi_id = $_POST["cpmi_id$i"];
$beneficiary_name = $_POST["beneficiary_name$i"];
$meeting_status = $_POST["meeting_status$i"];
$parent_name = $_POST["parent_name$i"];
$relation = $_POST["relation$i"];
$mobile_no = $_POST["mobile_no$i"];
$discussion_remark = $_POST["discussion_remark$i"];
if ($cpmi_id > 0) {
$update_query = "UPDATE child_parents_meeting
SET beneficiary_name = '$beneficiary_name', meeting_status = '$meeting_status', parent_name = '$parent_name',relation ='$relation',mobile_no = '$mobile_no', discussion_remark = '$discussion_remark', modified_by = '$modified_by'
WHERE cpmi_id = '$cpmi_id'";
if (!mysqli_query($conn, $update_query)) {
throw new Exception("Failed to update child_parents_meeting: " . mysqli_error($conn));
}
} else {
$query2 = "INSERT INTO child_parents_meeting (beneficiary_name, meeting_status, parent_name,relation,mobile_no, discussion_remark, ppm_id, modified_by)
VALUES ('$beneficiary_name', '$meeting_status', '$parent_name','$relation','$mobile_no', '$discussion_remark', '$ppm_id', '$modified_by')";
if (!mysqli_query($conn, $query2)) {
throw new Exception("Failed to insert into child_parents_meeting: " . mysqli_error($conn));
}
}
}
// Commit transaction
mysqli_commit($conn);
// Return data after successful insertion or update
$data = ['ppm_id' => $ppm_id];
echo json_encode($data);
} catch (Exception $e) {
mysqli_rollback($conn); // Rollback transaction on failure
error_log($e->getMessage());
header('HTTP/1.1 500 Internal Server Error');
die(json_encode(['error' => $e->getMessage()]));
}
?>