私は、必要なすべての変数を取得するアプリケーションの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)")
}
}
}
}
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);
コンソール版画 "接続成功"、と私は、接続がよくやっていると思う理由です(私は、しかしわかりません私はSwiftにかなり新しいです)
ありがとうございます@meda。 PHPページはそれをテストするだけです。それが動作するとき、私はより安全なスクリプトを作成します。私はあなたのコードを試し、結果を教えてくれるでしょう。ありがとうございました。 – alberzyzz
@alberzyzzこれはテストされていないことを覚えておいてください。私の指摘はparamStringを構築することです。私の答えの最後の行でエンコーディングを行ったためエンコードを取り除くことができます – meda
テストされていませんが動作しました!ありがとう、たくさんの男。 – alberzyzz