SQL Insert Funktion wird einmal aufgerufen aber ich habe zwei Inserts

    Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

    • SQL Insert Funktion wird einmal aufgerufen aber ich habe zwei Inserts

      Hallo zusammen,
      ich möchte mein Device in einer Datenbank registrieren. Dies soll beim Programmstart einmalig gemacht werden, dachte mir ich mache das einfach bei NavigationViewcontroller

      Das Problem ist jetzt dass ich immer zwei Einträge meiner UUID in meiner Datenbank habe, obwohl ich die Funktion nur einmal aufrufe

      PHP Code

      PHP-Quellcode

      1. <?php
      2. if($_SERVER["REQUEST_METHOD"]=="POST"){
      3. include 'connection.php';
      4. showTrace();
      5. }
      6. function showTrace()
      7. {
      8. global $pdo;
      9. $sqlString = $_POST["SQL"];
      10. $statement = $pdo->prepare($sqlString);
      11. $statement->execute();
      12. if (!$statement) {
      13. echo json_encode($dbh->errorInfo());
      14. }
      15. else
      16. {
      17. $statement->execute();
      18. $neue_id = $pdo->lastInsertId();
      19. echo json_encode($neue_id);
      20. }
      21. }
      22. ?>
      Alles anzeigen

      Swift Code:


      PHP-Quellcode

      1. class MainViewController: UINavigationController {
      2. override func viewDidLoad() {
      3. super.viewDidLoad()
      4. print("a")
      5. let sqlString = "INSERT INTO SWP_DEVICE (IMEI, FACTORY_ID) VALUES ('" + UIDevice.current.identifierForVendor!.uuidString + "', 1)"
      6. SQL_INSERT(SQL: sqlString)
      7. }
      8. }
      9. func SQL_INSERT(SQL: String)
      10. {
      11. let url = URL(string: "http://localhost/SQL_INSERT.php")!
      12. var request = URLRequest(url: url)
      13. request.httpMethod = "POST"
      14. let tmpStr = "SQL="+String(SQL)
      15. let postData = tmpStr.data(using: .utf8)
      16. request.httpBody = postData
      17. // execute the datatask and validate the result
      18. let task = URLSession.shared.dataTask(with: request) {
      19. (data, response, error) in
      20. if error != nil
      21. {
      22. print("Error")
      23. }
      24. else
      25. {
      26. print(data)
      27. if let content = data
      28. {
      29. do
      30. {
      31. let myJson2 = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
      32. print(myJson2)
      33. //let blogs = myJson2["qsm"] as? [[String: Any]]
      34. let blogs = myJson2 as? [[String: Any]]
      35. print(blogs as Any)
      36. for blog in blogs! {
      37. }
      38. }
      39. catch
      40. {
      41. }
      42. }
      43. }
      44. }
      45. task.resume()
      46. }
      Alles anzeigen