2016-05-21 12 views
-1

アップロードしたい評価の辞書(["volleyball":5、 "soccer":4、 "football":3])を持っています私のアプリケーション上で実行されているphpスクリプトを通して、既存のユーザデータベースを更新します。言い換えれば、私は、私が更新しようとしているテーブルUserの一部である私のSQLデータベースに "ratings"というBLOB値を持っています。何らかの理由で、「正しい形式でないため、データを読み取ることができませんでした」というエラーメッセージが表示され続けます。私は過去6時間ほどこのことに取り組んでいて、どこにも手を出していません。どんな助けも素晴らしいだろう!ありがとう。PHPを使用してSQLデータベースのBLOB値に辞書値を保存する

私の "MySQLDao.php"スクリプトにはこの機能があります。

public function addRatingsToUser($email, $ratings) { 
    $serialized_data = serialize($ratings); 
    $sql = "UPDATE User SET ratings=? WHERE email=?"; 
    $statement = $this->$conn->prepare($sql); 

    if (!$statement) throw new Exception($statement->error); 

    $statement->bind_param("ss", $serialized_data, $email); 
    $returnValue = $statement->execute(); 

    return $returnValue; 
} 

そして、これが私のメインのPHPスクリプトで、 "addUserRatings.php":

<?php 

require 'Conn.php'; 
require 'MySQLDao.php'; 

$data = json_decode(file_get_contents('php://input'), true); 
$returnValue = array(); 

$email = $data["email"]; 
$ratings = $data["ratings"]; 

$returnValue["status"] = "empty"; 
$returnValue["message"] = "empty"; 

$dao = new MySQLDao(); 
$dao->openConnection(); 

$result = $dao->addRatingsToUser($email, $ratings); 
if ($result) { 
    $returnValue["status"] = "success"; 
    $returnValue["message"] = "Ratings have been added to the existing user."; 

    echo json_encode($returnValue); 
    return; 
} 

echo json_encode($returnValue); 

$dao->closeConnection(); 

?> 

そして、ここでは私のSWIFTコードです:だから

@IBAction func submitRatings(sender:UIButton!) { 
     let userEmail = "[email protected]" 
     let ratingsDictionary:[String:Int] = ["volleyball":Int(self.volleyballRating.text!)!, "soccer":Int(self.soccerRating.text!)!, "football":Int(self.footballRating.text!)!] 

     let myURL = NSURL(string: "http://155.246.193.177:8888/addUserRatings.php") 
     let request = NSMutableURLRequest(URL: myURL!) 
     request.HTTPMethod = "POST" 

     let param = ["email":userEmail, "ratings":ratingsDictionary] 
     request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
     request.addValue("application/json", forHTTPHeaderField: "Accept") 

     do { 
      request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(param, options: NSJSONWritingOptions.PrettyPrinted) 
     } catch _ { 
      print("An error occurred with JSONSerialization.dataWithJSONObject") 
     } 


     let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) in 
      if error != nil { 
       print("\n\nError, \(error?.localizedDescription)") 
       return; 
      } 

      print("\n\nResponse = \(response)") 

      do { 
       let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary 
       if let parseJSON = json { 
        print("\n\nStatus = \(parseJSON["status"])") 
        print("\n\nMessage = \(parseJSON["message"])") 
        print("\n\nReturn Value = \(parseJSON["json"])") 
       } 
       else { 
        print("\n\nThe json object was nil, something went wrong. Maybe the server isn't running?") 
       } 
      } catch let err as NSError { 
       print("\n\nError with do, \(err.localizedDescription)") 
       print("\n\nData: \(data)") 
      } 
     } 

     task.resume() 
    } 

答えて

0

、古典的な構文エラー...私のaddRatingsToUser関数では、

の代わりに

$ステートメント= $ this-> $ conn-> prepare($ sql);

それは

$声明=の$ this - > conn->準備($ sqlを)する必要があります。

connの前にドル記号がありました。

+0

前に '$ conn = 'conn''を追加するだけです:-) – Alex

関連する問題