gpt4 book ai didi

php - POST 不工作 SWIFT PHP

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:05:27 24 4
gpt4 key购买 nike

<分区>

我在使用 Swift 3.0 的 Xcode 上遇到问题,我想用 php 将信息存储在 mysql 数据库中。已到达 php 代码,但总是在插入请求时返回失败。我不知道,它在安卓上运行良好。这是我的代码:

swift :

@IBAction func SignUp(_ sender: UIButton) {
var Name1: String = Name.text!
var Prenom: String = PRENAME.text!
var add: String = Addr.text!
var code:String = CP.text!
var mail:String = Email.text!
var pass:String = password.text!


var request = URLRequest(url: URL(string: "http://www.example.com/myscript.php")!)
request.httpMethod = "POST"
let postString = "name=\(Name1)&pname=\(Prenom)&add=\(add)&pc=\(code)&mail=\(mail)&pass=\(pass)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
}
task.resume()
}

还有我的 PHP 代码:

<?php

$reponse = array();



$pseudo = $_POST['name'];
$score = $_POST['pname'];
$add = $_POST['add'];
$pc = $_POST['pc'];
$mail = $_POST['mail'];
$password = $_POST['pass'];
$mysqli = new MySQLi("host","root","pass","user");

$result = $mysqli->query("INSERT INTO compte (Name,Name2,Ad,code,maiil,pass) VALUES('$pseudo','$score','$add','$pc','$mail','$password')");
if ($mysqli->connect_error){
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
if($result){

$response["success"] = 1;
$response["message"] = "SUCCESS!";

echo json_encode($response);
}
else{
$response["success"] = 0;
$response["message"] = "FAILURE!";

echo json_encode($response);
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com