首页 > 解决方案 > 为 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 脚本的整体连接似乎有效。

标签: phpmysqlswift

解决方案


您正在使用 POST 方法,因此您应该获取 POST 值而不是 GET 值:

if($_SERVER['REQUEST_METHOD']=='POST'){

    //getting values
    $teamName = $_POST['name'];
    $memberCount = $_POST['member'];

推荐阅读