0
  1. this is my php api code :

    header('Content-Type: application/json');
    $return_arr = array();  
    $username=mysqli_real_escape_string($link,$_POST["name"]);
    $email=mysqli_real_escape_string($link,$_POST["email"]);
    $mobile=mysqli_real_escape_string($link,$_POST["mobile"]);
    $password=mysqli_real_escape_string($link,$_POST["password"]);
    
    $result=mysqli_query($link,"insert into users(name,email,mob,password) values('$username','$email','$mobile','$password')");    
    if(mysqli_num_rows($result))
    {
    $row_array['status']=true;
    array_push($return_arr,$row_array);
    }
    else 
    {
    $row_array['status']=false;
    array_push($return_arr,$row_array);
    }
    echo json_encode($return_arr);
    mysqli_close($link);
    
  2. Nil entry is inserting into a database. where is the actual problem, I test all but can't get.

  3. This code is not working, how url encoding possible in this code:

    let signUpUrl = URL(string: "myURL/api_register.php")
    var request = URLRequest(url: signUpUrl!)
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")
    
    
    let postString : Parameters = ["name": "com", "email": "raj123@v", "mobile": "123", "password": "123"]
    print("\(postString)")
    do {
        request.httpBody = try JSONSerialization.data(withJSONObject: postString, options: .prettyPrinted)
        //print(request.description)
    } catch let error {
        print(error.localizedDescription)
        displayMessage(userMessage: "Something went wrong")
        return
    }
     let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
    
    
    
        if error != nil {
            self.displayMessage(userMessage: "Count perform this request, Please try again later")
            print("Error desription  \(String(describing: error))")
            return
        }
        guard let data = data else{
            print("no data return")
            return
        }
        //Let's convert response send from the server side to NSDictionary
        do {
            let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
            print("json is..\(String(describing: json))")
    
            // Now we can access value of User ID by its key
            if let printedJson = json {
                print("print... \(printedJson)")
                let userId = printedJson["success"] as? Bool
                print("user id is \(String(describing: userId))")
                if !userId! {
    
                    //self.displayMessage(userMessage: "Cound not perform this request, Please try again.")
                    //return
                    print("no.")
                } else {
                    //Redirect to Home Screen----------------------------
                    print("yes")
    
                    // let appdelegate = UIApplication.shared.delegate
                    //  appdelegate?.window??.rootViewController = homepage
                }
            } else {
                self.displayMessage(userMessage: "Cound not perform this request, Please try again...")
                return
            }
    
    
        } catch {
    
            self.displayMessage(userMessage: "Cound not perform this request, Please try again.")
            print("error\(error)")
            return
    
        }
    
    
    }
    task.resume()
    
  4. when I used alarmofire.request with URLEncoding.default method its work but using this It doesn't. Something is missing in this code.

  • You didn't write code to call Webservice – Prashant Tukadiya Mar 24 '18 at 13:37
  • Possible duplicate of [Swift return data from URLSession](https://stackoverflow.com/questions/43048120/swift-return-data-from-urlsession) – Prashant Tukadiya Mar 24 '18 at 13:40
  • You have added a good load of somewhat functional (but unSwifty which is to be expected from a newbie) code, but you have not clearly stated your problem. Could you add the output of the (commented) line ` print(request.description)`? It might help us to judge wether your problem is on the Swift or the PHP-side. Could you also output what you get from the request in PHP? – Patru Mar 24 '18 at 14:17
  • I added some description and code , please check it –  Mar 26 '18 at 05:00
  • @PrashantTukadiya this is not answer of this question –  Mar 26 '18 at 05:06
  • Thank you for adding more error handling (which is a good idea in any case). However, it is still not clear what your sending code is actually sending and what your PHP code will receive (if anything at all). From my limited understanding of PHP it might expect some `form` data, but you are trying to pass it some JSON?? Please add the output of both sides. – Patru Mar 27 '18 at 03:13

0 Answers0