Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php swift 2字符“&&引用;无法在http POST请求中上载_Php_Mysql_Swift2 - Fatal编程技术网

Php swift 2字符“&&引用;无法在http POST请求中上载

Php swift 2字符“&&引用;无法在http POST请求中上载,php,mysql,swift2,Php,Mysql,Swift2,我试图通过PHP向mysql发送一些字符串,该字符串包含一些特殊字符,如“&、、/”。 问题是字符“&”后面的所有字符串都将丢失。 例如,字符串“Ak-47&Bomber”将在“Ak-47”中发送 斯威夫特: let url: NSURL = NSURL(string: "MyPhP.php")! let session = NSURLSession.sharedSession() let request:NSMutableURLRequest = NSMut

我试图通过PHP向mysql发送一些字符串,该字符串包含一些特殊字符,如“&、、/”。 问题是字符“&”后面的所有字符串都将丢失。 例如,字符串“Ak-47&Bomber”将在“Ak-47”中发送

斯威夫特:

 let url: NSURL = NSURL(string: "MyPhP.php")!
        let session = NSURLSession.sharedSession()

        let request:NSMutableURLRequest = NSMutableURLRequest(URL:url)
        request.HTTPMethod = "POST"
        request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData

        let bodydata = "String=\(Ak-47&Bomber)"
        request.HTTPBody = bodydata.dataUsingEncoding(NSUTF8StringEncoding)

        if bodydata != " " {

            let task = session.dataTaskWithRequest(request) {
                (data, response, error)  -> Void in

                let httpResponse = response as! NSHTTPURLResponse
                let statusCode = httpResponse.statusCode
PHP:


有解决办法吗

您需要对数据进行URL编码。我已经尝试了,
让str=Ak-47&Bomber
让str2=str.String,方法是添加PercentEncoding with AllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())
让bodydata=“String=\(str2)”
,但是,它不起作用。我还尝试了
$String=htmlspecialchars($\u POST[“String”];
现在我使用
stringByAddingPercentEncodingWithAllowedCharacters
,它完成了,但我不知道如何解码它。
$String= ($_POST["String"]); // the String i got "Ak-47"
$sql =  "INSERT INTO table (string) VALUES ('" .
            $String."' )";
        $result = mysqli_query($conn, $sql);