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
Alles anzeigen
Swift Code:
Alles anzeigen
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
- <?php
- if($_SERVER["REQUEST_METHOD"]=="POST"){
- include 'connection.php';
- showTrace();
- }
- function showTrace()
- {
- global $pdo;
- $sqlString = $_POST["SQL"];
- $statement = $pdo->prepare($sqlString);
- $statement->execute();
- if (!$statement) {
- echo json_encode($dbh->errorInfo());
- }
- else
- {
- $statement->execute();
- $neue_id = $pdo->lastInsertId();
- echo json_encode($neue_id);
- }
- }
- ?>
Swift Code:
PHP-Quellcode
- class MainViewController: UINavigationController {
- override func viewDidLoad() {
- super.viewDidLoad()
- print("a")
- let sqlString = "INSERT INTO SWP_DEVICE (IMEI, FACTORY_ID) VALUES ('" + UIDevice.current.identifierForVendor!.uuidString + "', 1)"
- SQL_INSERT(SQL: sqlString)
- }
- }
- func SQL_INSERT(SQL: String)
- {
- let url = URL(string: "http://localhost/SQL_INSERT.php")!
- var request = URLRequest(url: url)
- request.httpMethod = "POST"
- let tmpStr = "SQL="+String(SQL)
- let postData = tmpStr.data(using: .utf8)
- request.httpBody = postData
- // execute the datatask and validate the result
- let task = URLSession.shared.dataTask(with: request) {
- (data, response, error) in
- if error != nil
- {
- print("Error")
- }
- else
- {
- print(data)
- if let content = data
- {
- do
- {
- let myJson2 = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
- print(myJson2)
- //let blogs = myJson2["qsm"] as? [[String: Any]]
- let blogs = myJson2 as? [[String: Any]]
- print(blogs as Any)
- for blog in blogs! {
- }
- }
- catch
- {
- }
- }
- }
- }
- task.resume()
- }