getConnection(); // This returns a PDO instance // Gather POST data $branchCode = $_POST['branchcode'] ?? ''; $loCode = $_POST['loname'] ?? ''; $aadharNumber = $_POST['empadhar_number'] ?? ''; $aadharFile = $_POST['fileName'] ?? ''; // From hidden input for Aadhar $policeFile = $_POST['fileNamePolice'] ?? ''; // From hidden input for Police Verification $otherFile = $_POST['fileNameOther'] ?? ''; // From hidden input for Other Documents // Remove spaces from the Aadhar number $aadharNumber = str_replace(' ', '', $aadharNumber); // Validate required fields if ($branchCode == '-1' || empty($aadharNumber)) { // Redirect back with an error message header("Location: dashboard.employee.reg.php?status=error&message=Required fields are missing."); exit; } try { // Check if Aadhar number already exists $checkQuery = "SELECT COUNT(*) FROM acts_emp WHERE empadharnumber = :aadharNumber"; $stmtCheck = $conn->prepare($checkQuery); $stmtCheck->bindParam(':aadharNumber', $aadharNumber, PDO::PARAM_STR); $stmtCheck->execute(); $recordExists = $stmtCheck->fetchColumn(); // Get the count if ($recordExists > 0) { // If record exists, update it $query = "UPDATE acts_emp SET empbranchcode = :branchCode, loname = :loCode, doc_adhar = :aadharFile, doc_polveri = :policeFile, doc_other = :otherFile WHERE empadharnumber = :aadharNumber"; } else { // If record does not exist, insert a new one $query = "INSERT INTO acts_emp (empbranchcode, loname, empadharnumber, doc_adhar, doc_polveri, doc_other) VALUES (:branchCode, :loCode, :aadharNumber, :aadharFile, :policeFile, :otherFile)"; } $stmt = $conn->prepare($query); // Bind parameters $stmt->bindParam(':branchCode', $branchCode, PDO::PARAM_INT); $stmt->bindParam(':loCode', $loCode, PDO::PARAM_STR); $stmt->bindParam(':aadharNumber', $aadharNumber, PDO::PARAM_STR); $stmt->bindParam(':aadharFile', $aadharFile, PDO::PARAM_STR); $stmt->bindParam(':policeFile', $policeFile, PDO::PARAM_STR); $stmt->bindParam(':otherFile', $otherFile, PDO::PARAM_STR); // Execute query if ($stmt->execute()) { echo json_encode(['success' => true, 'message' => 'Data saved successfully!']); } else { echo json_encode(['success' => false, 'message' => 'Error saving data.']); } exit; } catch (PDOException $e) { // Redirect back with database error message header("Location: dashboard.employee.reg.php?status=error&message=Database error: " . $e->getMessage()); exit; } } else { // Invalid request method header("Location: dashboard.employee.reg.php?status=error&message=Invalid request method."); exit; }