setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Prepare a query to check if the entry already exists in erfnumbers $queryCheck = "SELECT COUNT(*) FROM erfnumbers WHERE TAG_X = :TAG_X AND TAG_Y = :TAG_Y"; $stmtCheck = $db2->prepare($queryCheck); $stmtCheck->bindParam(':TAG_X', $TAG_X); $stmtCheck->bindParam(':TAG_Y', $TAG_Y); $stmtCheck->execute(); $exists = $stmtCheck->fetchColumn(); // If the entry does not exist, insert the new entry if ($exists == 0) { $queryInsert = "INSERT INTO erfnumbers (erfno, allotment, projectname, TAG_X, TAG_Y) VALUES (:erfno, :allotment, :projectname, :TAG_X, :TAG_Y)"; $stmtInsert = $db2->prepare($queryInsert); $stmtInsert->bindParam(':erfno', $erfno); $stmtInsert->bindParam(':allotment', $allotment); $stmtInsert->bindParam(':projectname', $projectname); $stmtInsert->bindParam(':TAG_X', $TAG_X); $stmtInsert->bindParam(':TAG_Y', $TAG_Y); $stmtInsert->execute(); // Return success response echo json_encode(['status' => 'success', 'message' => 'New entry added successfully.']); } else { // Return entry already exists response echo json_encode(['status' => 'exists', 'message' => 'Entry already exists for Erfno: ' . $erfno . ' and Allotment: ' . $allotment]); } } catch (PDOException $e) { // Return error response in case of database error echo json_encode(['status' => 'error', 'message' => $e->getMessage()]); } } } ?>