PHP脚本未将Swift Data插入MYSQL数据库

时间:2018-11-04 23:24:00

标签: php mysql swift post

在IOS应用程序中,我试图获取用户输入并将其提交到MYSQL数据库,但是该数据库未更新。这是用来提交数据的swift函数:

  func submit_data(){
    let request = NSMutableURLRequest(url: NSURL(string: "omitted")! as URL)
    request.httpMethod = "POST"
    let postString = "a=\(bar_name)&b=\(user_data)"
    request.httpBody = postString.data(using: String.Encoding.utf8)     
    let task = URLSession.shared.dataTask(with: request as URLRequest) {
        data, response, error in      
        if error != nil {
            print("error=\(error)")
            return
        }           
        print("response = \(response)")        
        let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        print("responseString = \(responseString)")     
        print(postString)    
    }
    task.resume()  
}

这是我的PHP代码:

<?PHP
//Create connection
$con=mysqli_connect("localhost","username","password","covermeo_coverme_data");
//check connection

if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "INSERT INTO User_Data (Bar, Cover)
VALUES ('".$_POST["a"]."','".$_POST["b"]."')";
$result = mysqli_query($conn,$sql);

// Close connections
mysqli_close($con);
?>

Here is a screenshot of the MYSQL database.

1 个答案:

答案 0 :(得分:0)

解决方案:

  1. 打开Info.plist
  2. 添加密钥.outer{ background:yellow; height:100px; margin-bottom:16px; }
  3. 添加子键App Transport Security Settings布尔值-Allow Arbitrary Loads

like this