prepare($query2); $stmt->bindValue(':selectedName', $selectedName, SQLITE3_TEXT); $result2 = $stmt->execute(); $data = $result2->fetchArray(SQLITE3_ASSOC); // Close the database connection $db1->close(); if ($data) { $address = $data['address']; $mobile = $data['mobile']; $email = $data['email']; $clientid = $data['id']; } else { $successfull = "Select a client!"; echo json_encode(['successfull' => $successfull]); // die("Client not found in the database."); exit; } if ($projectname == "" || $projectname == "Create a project") { // die("Invalid data submitted."); $successfull = "Select a project!"; echo json_encode(['successfull' => $successfull]); exit; } $checkvalues = preg_replace("/[^0-9]+/", "", $subtotal); $checkvalues = intval($checkvalues); $checkvalues = strval($checkvalues); // Check for data validity if (empty($streetname_no) || empty($item) || empty($subtotal) || $subtotal == "0.00" || $subtotal == "" || $subtotal == "0") { // die("Invalid data submitted."); $successfull = "Fill in all fields!"; echo json_encode(['successfull' => $successfull]); exit; } if (!$checkvalues){ $successfull = "Fill in all fields!"; echo json_encode(['successfull' => $successfull]); exit; } if ($checkvalues == "0"){ $successfull = "Fill in all fields!"; echo json_encode(['successfull' => $successfull]); exit; } $projectNumber = ""; // Connect to the SQLite3 database $db2 = new SQLite3('database/projects.db'); if (!$db2) { die("Failed to connect to the database."); } $query3 = "SELECT projectname, erfno, allotment, client, clientid, clientprogress FROM projects"; $result1 = $db2->query($query3); while ($row1 = $result1->fetchArray()) { $myArray1 = explode('«', $row1['clientprogress']); if ($projectname == $row1['projectname']){ $projectNumber = $myArray1[2]; } } $db2->close(); // Connect to the SQLite database $db = new SQLite3('database/invoices.db'); if (!$db) { die("Failed to connect to the database."); } // Prepare and execute the INSERT query to add the client to the database $insertQuery = "INSERT INTO figures (item, description, subtotal, clientname, mobile, email, address, projectname, invoicefull, createdby, createdate, totalvalue, clientid, status, projectNumber) VALUES (:item, :description, :subtotal, :clientname, :mobile, :email, :address, :projectname, :invoiceNumber, :createdby, :createdate, :totalValue, :clientid, :status, :projectNumber)"; $stmt = $db->prepare($insertQuery); $stmt->bindValue(':item', $item, SQLITE3_TEXT); $stmt->bindValue(':description', $description, SQLITE3_TEXT); $stmt->bindValue(':subtotal', $subtotal, SQLITE3_TEXT); $stmt->bindValue(':clientname', $selectedName, SQLITE3_TEXT); // Assuming client name should be the same as selectedName $stmt->bindValue(':mobile', $mobile, SQLITE3_TEXT); $stmt->bindValue(':email', $email, SQLITE3_TEXT); $stmt->bindValue(':address', $address, SQLITE3_TEXT); $stmt->bindValue(':projectname', $projectname, SQLITE3_TEXT); $stmt->bindValue(':invoiceNumber', $invoiceNumber, SQLITE3_TEXT); $stmt->bindValue(':createdby', $createby, SQLITE3_TEXT); $stmt->bindValue(':createdate', $createdate, SQLITE3_TEXT); $stmt->bindValue(':totalValue', $totalValue, SQLITE3_TEXT); $stmt->bindValue(':clientid', $clientid, SQLITE3_TEXT); $stmt->bindValue(':status', $status, SQLITE3_TEXT); $stmt->bindValue(':projectNumber', $projectNumber, SQLITE3_TEXT); // Execute the query $result = $stmt->execute(); if ($result) { $successfull = "Successfull"; echo json_encode(['successfull' => $successfull]); } else { $successfull = "Not Successfull"; echo json_encode(['successfull' => $successfull]); } } ?>