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.

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站