dth34653 2015-11-17 14:26
浏览 48

特殊字符插入DB

I'm having issues inserting special characters into my database. For example if the string is "\kdjfg*&(^^&%%//""dfkjs/Z?!" then "\kdjfg*" would be inserted into the table. Im not sure why the whole string is not getting inserted when special characters are present

Swift:

let post:NSString = "a=\(a)&b=\(b)&c=\(c)&d=\(d)&username=\(username)";
    let url:NSURL = NSURL(string:PassURL)!
    let postData = post.dataUsingEncoding(NSUTF8StringEncoding)!

    let postLength = String(postData.length)
    //Setting up `request` is similar to using NSURLConnection
    let request = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST"
    request.HTTPBody = postData
    request.setValue(postLength, forHTTPHeaderField: "Content-Length")
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request.setValue("application/json", forHTTPHeaderField: "Accept")


    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithRequest(request) {urlData, response, reponseError in

        if let receivedData = urlData {
            let res = response as! NSHTTPURLResponse!;

            NSLog("Response code: %ld", res.statusCode);

            if (res.statusCode >= 200 && res.statusCode < 300) {
                do {
                    let jsonData = try NSJSONSerialization.JSONObjectWithData(receivedData, options: []) as! NSDictionary
                    //On success, invoke `completion` with passing jsonData.
                    completion(jsonData: jsonData, error: nil)
                } catch {
                    //On error, invoke `completion` with NSError.
                    completion(jsonData: nil, error: nil)
                }                      }
            else
            {
                completion(jsonData: nil, error: nil)
            }
        }
    }
    task.resume()
}

php:

header('Content-type: application/json');
if($_POST) {
    $username   = $_POST['username'];
    $a   = $_POST['a'];
    $b   = $_POST['b'];
    $c   = $_POST['c'];
    $d   = $_POST['d'];
    $mysqli = new mysqli($server_url, $db_user, $db_password, $db_name);

        /* check connection */
        if (mysqli_connect_errno()) {
            error_log("Connect failed: " . mysqli_connect_error());
            echo '{"success":0,"error_message":"' . mysqli_connect_error() . '"}';
        } else {

 $stmt = $mysqli->prepare("insert into testTable (a,b,c,d,postedby) VALUES (?,?,?,?,?)");
 $stmt->bind_param("sssss",$a,$b,$c,$d,$username);
 $stmt->execute();
 $success = $stmt->affected_rows;
 $id = $stmt->insert_id;
  • 写回答

1条回答 默认 最新

  • dongtangdao7232 2015-11-17 15:06
    关注

    I'm not sure if this is the proper way of handling special characters but this is how I got over the goal line.

    I stuffed the special character in iOS using:

    a.stringByReplacingOccurrencesOfString("&", withString: "(*)*#@#@$#")
    

    Then on the php side, I replace ()#@#@$# with &

    $newString = str_replace("(*)*#@#@$#","&",$a);
    

    I'm hoping someone else can provide a better way in the future

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?