prepare("INSERT INTO invoices (projectname, invoicedate, movevalue, invoiceid, type, totalvalue) VALUES (:projectname, :invoicedate, :movevalue, :invoiceid, :type, :totalvalue)"); // Bind the data to the SQL statement $stmt->bindParam(':projectname', $projectname); $stmt->bindParam(':invoicedate', $invoicedate); $stmt->bindParam(':movevalue', $movevalue); $stmt->bindParam(':invoiceid', $invoiceid); $stmt->bindParam(':type', $type); $stmt->bindParam(':totalvalue', $totalvalue); // Execute the SQL statement if ($stmt->execute()) { echo json_encode(['status' => 'success', 'message' => $projectname. ' - Invoice inserted successfully']); ; } else { echo json_encode(['status' => 'error', 'message' => 'Failed to insert invoice!']); } } else { // Check if the invoiceid already exists $checkStmt = $db->prepare("SELECT COUNT(*) FROM invoices WHERE invoiceid = :invoiceid"); $checkStmt->bindParam(':invoiceid', $invoiceid); $checkStmt->execute(); $exists = $checkStmt->fetchColumn(); if ($exists) { echo json_encode(['status' => 'error', 'message' => 'Invoice ID already exists!']); } else { // Prepare an SQL statement to insert the data $stmt = $db->prepare("INSERT INTO invoices (projectname, invoicedate, movevalue, invoiceid, type, totalvalue) VALUES (:projectname, :invoicedate, :movevalue, :invoiceid, :type, :totalvalue)"); // Bind the data to the SQL statement $stmt->bindParam(':projectname', $projectname); $stmt->bindParam(':invoicedate', $invoicedate); $stmt->bindParam(':movevalue', $movevalue); $stmt->bindParam(':invoiceid', $invoiceid); $stmt->bindParam(':type', $type); $stmt->bindParam(':totalvalue', $totalvalue); // Execute the SQL statement if ($stmt->execute()) { echo json_encode(['status' => 'success', 'message' => 'Invoice inserted successfully!']); } else { echo json_encode(['status' => 'error', 'message' => 'Failed to insert invoice!']); } } } } else { echo json_encode(['status' => 'error', 'message' => 'Invalid request method']); } ?>