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

时间:2016-12-29 17:26:11

标签: php ios swift

我有一个应用程序的VC,它可以获取所需的所有变量,并且应该通过POST请求将它们传递给php文件,然后将它们存储并发送到数据库。当数据库中没有设置变量时,问题出现了(我相信连接做得很好)。 php文件在Android应用程序中工作正常(使用Android应用程序可以很好地存储变量)。

如果你能给我一些帮助,我将不胜感激。

夫特

@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)")
            }
        }
    }
}

$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);

控制台打印"连接成功",这就是我认为连接完成的原因(我不确定,因为我对Swift很新)

1 个答案:

答案 0 :(得分:1)

您一遍又一遍地覆盖了httpBody的请求。

最重要的是,你没有传递与你的帖子变量的keys匹配的values

你需要这些内容:

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)

PHP我看不到验证,如果您没有验证,那么您应该添加它,因为它与您的数据库交互。

更不用说您接触SQL次注射。