php - 为 PHP 脚本的 POST 请求创建任务以在 MySQL 数据库中添加条目
问题描述
我想通过 swift 5 实现一个 IOS 应用程序来调整由 PHP 脚本触发的 MySQL 数据库。我尝试使用 post 请求来移交以下参数: teamName = "Testteam" teamCount = "member"
经过一些测试我发现,这些参数没有移交给 PHP 脚本(它们是空的),我不知道为什么。
SWIFT代码
let URL_SAVE_TEAM ="http://www.mydomain.de/createteam.php"
@IBOutlet weak var textFieldName: UITextField!
@IBOutlet weak var textFieldMember: UITextField!
@IBAction func Login(_ sender: UIButton) {
//created NSURL
let requestURL = NSURL(string: URL_SAVE_TEAM)!
//creating NSMutableURLRequest
let request = NSMutableURLRequest(url: requestURL as URL)
//setting the method to post
request.httpMethod = "POST"
//getting values from text fields
let teamName = textFieldName.text
let memberCount = textFieldMember.text
//creating the post parameter by concatenating the keys and values from text field
//let postParameters = "name="+teamName!+"&member="+memberCount!;
//adding the parameters to request body
request.httpBody = postParameters.data(using: String.Encoding.utf8)
//creating a task to send the post request
let task = URLSession.shared.dataTask(with: request as URLRequest) {
(data, response, error) in
if error != nil {
print("error is \(String(describing: error))")
return
}
do { //parsing the response
let myJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary //converting resonse to NSDictionary
print(myJSON)
//parsing the json
if let parseJSON = myJSON {
//creating a string
var msg : String!
//getting the json response
msg = parseJSON["message"] as! String?
//printing the response
print(msg!)
}
} catch {
print(error)
}
}
//executing the task
task.resume()
}
服务器上的 PHP 脚本
<?php
//creating response array
$response = array();
if($_SERVER['REQUEST_METHOD']=='POST'){
//getting values
$teamName = $_GET['name'];
$memberCount = $_GET['member'];
$fp = fopen ('debug.log' , "w"); // Datei öffnen
fwrite ($fp , 'test'); // Dateiinhalt in die Datei schreiben
fwrite ($fp , $teamName); // Dateiinhalt in die Datei schreiben
fwrite ($fp , $memberCount); // Dateiinhalt in die Datei schreiben
fclose ($fp); // Datei schließen
//including the db operation file
require_once '../includes/DbOperation.php';
$db = new DbOperation();
//inserting values
if($db->createTeam($teamName,$memberCount)){
$response['error']=false;
$response['message']='Team added successfully';
}else{
$response['error']=true;
$response['message']='Could not add team';
}
}else{
$response['error']=true;
$response['message']='You are not authorized';
}
echo json_encode($response);
?>
PHP 脚本正在发送错误消息(无法添加团队),因为变量 $teamName 和 $memberCount 为空。与 PHP 脚本的整体连接似乎有效。
解决方案
您正在使用 POST 方法,因此您应该获取 POST 值而不是 GET 值:
if($_SERVER['REQUEST_METHOD']=='POST'){
//getting values
$teamName = $_POST['name'];
$memberCount = $_POST['member'];
推荐阅读
- file - 将 Teoco Asset .3ga 扩展名转换为标准文件格式,如 .lib 或 .csv 或其他
- php - PHP Laravel BaseRepository CURL 错误 [ curl_setopt_array(): 提供的资源不是有效的 cURL 句柄资源]
- android - Android通知栏不更新所有值
- c# - c#拦截构造函数参数
- c - getchar 可以扫描的一行中有多少个字符有限制吗?
- html - Bootstrap 4 下拉菜单 - 自动调整宽度?
- php - 带连接的求和函数是用 mysql 将 laravel ORM 中条目的总和乘以 mysql
- android - 阿拉伯语自然语言处理项目的深度学习
- javascript - 对于 NoUiSlider,如何设置手柄/拇指的宽度?
- android - 如何将编辑后的 AutoCompleteTextField 值转换为表单数据