2016-12-29 80 views
0

我有一個應用程序的VC,獲取所有需要的變量,並應通過POST請求將它們傳遞到一個PHP文件,在那裏它們被存儲併發送到數據庫。問題出現在數據庫中沒有設置變量(我相信連接已經完成)。這個php文件和一個Android應用程序一樣工作正常(所以變量使用Android應用程序很好地存儲)。快速發佈POST請求沒有什麼

如果您能給我一些幫助,我將不勝感激。

斯威夫特

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

控制檯打印 「連接成功」,這就是爲什麼我認爲連接做得很好(我不知道,雖然,因爲我對斯威夫特很新穎)

回答

1

你壓倒一次又一次的210請求。

最重要的是,您沒有通過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注射劑。

+0

感謝您的時間@meda。該PHP頁面只是爲了測試它。當它工作時,我會製作更安全的腳本。我會嘗試你的代碼並告訴你結果。再次感謝你。 – alberzyzz

+0

@alberzyzz記住它沒有測試,我的觀點是構建paramString,並且您可以刪除編碼,因爲我在我的答案的最後一行做了編碼 – meda

+0

未測試但工作!非常感謝你。 – alberzyzz