duan4523 2016-12-29 17:26
浏览 77
已采纳

POST请求在swift中没有发布任何内容

I have got a VC of an app that gets all the variables needed and should pass them through a POST request to a php file, where they are stored and sent to a database. The problem comes when the variables are not set in the database (I believe the connection is well done). The php file is working fine with an Android app that does the same (so the variables are well stored using the Android app).

I would be grateful if you could give me some help.

Swift

@IBAction func modPres(_ sender: AnyObject) {
    let postDataURL = "https://www.juankarfollador.com/login_app.php"
    let url: NSURL = NSURL(string: postDataURL)!
    let request: NSMutableURLRequest = NSMutableURLRequest(url:url as URL)

    request.httpMethod = "POST"
    request.httpBody = user.data(using: String.Encoding.utf8)
    request.httpBody = l_origen.data(using: String.Encoding.utf8)
    request.httpBody = l_destino.data(using: String.Encoding.utf8)
    request.httpBody = num_pal.data(using: String.Encoding.utf8)
    request.httpBody = String(precio).data(using: String.Encoding.utf8)
    request.httpBody = texto.data(using: String.Encoding.utf8)

    NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue.main)
    {
        (response, data, error) in
        print(response!)

        if let httpResponse = response as? HTTPURLResponse {
            let statusCode = httpResponse.statusCode

            if statusCode==200 {
                print("Connection Successful")

            } else {
                print("Connection Failed (!200)")
            }
        }
    }
}

Php

$precio = $_POST['precio']; 

 $texto = $_POST['texto']; 

 $user = $_POST['user']; 

 $l_origen = $_POST['l_origen']; 

 $l_destino = $_POST['l_destino'];

 $num_pal = $_POST['num_pal']; 

 $modificar = $_POST['modificar'];

 define('HOST','***');
 define('USER','***');
 define('PASS','***');
 define('DB','***');

 $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

mysqli_set_charset( $con, 'utf8');

//Cliente

 $sql = "UPDATE users SET precio='$precio', text_cli='$texto', l_origen='$l_origen', l_destino='$l_destino', num_pal='$num_pal' WHERE username='$user' AND text_cli=''";

 mysqli_query($con,$sql);

The console prints "Connection Successful", and this is why I think the connection is well done (I am not sure though, as I am pretty new to Swift)

  • 写回答

1条回答 默认 最新

  • drjmrg8766 2016-12-29 17:35
    关注

    You are overriding the httpBody of your request over and over.

    On top of that you are not passing the keys matching the values of your post variables.

    You need something along these lines:

    let paramString = "precio=\(precio)&texto=\(texto)&user=\(user)&l_origen=\(l_origen)&l_destino=\(l_destino)&num_pal=\(l_destino)&modificar=\(modificar)"
    request.httpMethod = "POST"
    request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)
    

    In your PHP I don't see a validation, if you don't have it then you should really add it because it interacts with your db.

    Not to mention that you are exposed to SQL injections.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效