数据没有插入swift 4中的数据库(nil数据插入带有空白条目的数据库中)

时间:2018-03-24 11:35:02

标签: ios json swift nsjsonserialization

  1. 这是我的php api代码:

    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条目正在插入数据库。实际问题在哪里,我测试了所有但是无法获得。

  3. 此代码无效,此代码中的url编码方式如何:

    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. 当我使用带有URLEncoding.default方法的alarmofire.request时,它的工作却使用了它 。这段代码中缺少一些东西。

0 个答案:

没有答案