2017-03-12 8 views
0

私はphp & mysqlにかなり新しく、ユーザーのプロフィールをmy dbに保存するための助けを求めたいと思います。 WebServiceクラスで実行php myql dbを使用してユーザープロファイルpicを保存します

@IBAction func usr_Tapped_Save(_ sender: Any) { 

    // After User has selected a Profile Pic 

    if let image = m_imgPhoto { 
     if let data = UIImagePNGRepresentation(image) { 
      let filename = getDocumentsDirectory().appendingPathComponent("copy.png") 
      try? data.write(to: filename) 
      let url = String(describing: filename) 

      SVProgressHUD.show(withStatus: "Please wait...") 
      WebService.sharedInstance().updateUser(UserGender: self.m_gender!, 
                UserBirthday: self.m_bday!, 
                UserStats: self.m_relStat!, 
                UserImage: url) 
      { (response, error) in 
       if let error = error { 
        SVProgressHUD.showError(withStatus: error) 
       } else { 
        SVProgressHUD.dismiss() 

        GlobalService.sharedInstance().g_userMe?.user_gender = self.m_gender! 
        GlobalService.sharedInstance().g_userMe?.user_birthday = self.m_bday! 
        GlobalService.sharedInstance().g_userMe?.user_stats = self.m_relStat! 
        GlobalService.sharedInstance().g_userMe?.user_image = url 
        GlobalService.sharedInstance().saveUserObj() 

        // move to profile pic screen 
        let ProfPicVC = self.storyboard?.instantiateViewController(withIdentifier: String(describing: EditProfileViewController.self)) as! EditProfileViewController 
        self.navigationController?.pushViewController(ProfPicVC, animated: true) 
       } 
      } 
     } 
    } 

のFunc

 func updateUser(UserGender: String, UserBirthday: Date, UserStats: String, UserImage: String, completion: @escaping (String?, String?) -> Void) { 
    let dicParams = [ 
     "user_gender" : UserGender, 
     "user_birthday" : UserBirthday, 
     "user_stats" : UserStats, 
     "user_image" : UserImage 
     ] as [String : Any] 

    Alamofire.request("\(Constants.Server.URL)/users", 
     method: .put, 
     parameters: dicParams, 
     headers: header) 
     .validate() 
     .responseJSON { (response) in 
      switch response.result { 
      case .success(let value as [String: String]): 
       completion(value[Constants.Server.RESPONSE_MESSAGE], nil) 
      case .failure(_): 
       completion(nil, self.getErrorString(ErrorData: response.data!)) 
      default: 
       completion(nil, "Unkown Error") 
      } 
    } 
} 

PHPスクリプト:ユーザーのタップが保存すると、現在、これは

スウィフト3を実行するコードがある

function updateUser($req, $res) { 
global $db; 

$user_id = validateUserAuthentication($req); 
if($user_id) { 
    $params = $req->getParams(); 

    $query = $db->prepare('update tblUser set user_gender = :user_gender, 
               user_birthday = :user_birthday, 
               user_stats = :user_stats, 
               user_image = :user_image 
             where user_id = :user_id'); 
    $query->bindParam(':user_id', $user_id); 
    $query->bindParam(':user_gender', $params['user_gender']); 
    $query->bindParam(':user_birthday', $params['user_birthday']); 
    $query->bindParam(':user_stats', $params['user_stats']); 
    $query->bindParam(':user_image', $params['user_image']); 

    if ($query->execute()) { 
     $newRes = makeResultResponseWithString($res, 200, 'User updated successfully'); 
    } else { 
     $newRes = makeResultResponseWithString($res, 400, $query->errorInfo()[2]); 
    } 
} else { 
    $newRes = makeResultResponseWithString($res, 401, 'Your token has expired. Please login again.'); 
} 

return $newRes; 
} 

私はまた、それを保存するデータ型の種類を尋ねたい、それは現在varchar(500)ですが、私はunsuです再。

<?php 
$connection=mysqli_connect("localhost","root","","imgup"); 
if(isset($_POST['create_post'])){ 
    $post_image = $_FILES['image']['name']; 
    $post_image_temp = $_FILES['image']['tmp_name']; 
    move_uploaded_file($post_image_temp, "images/$post_image"); 
    $query = "INSERT INTO img(post_image) "; 
    $query .= "VALUES('{$post_image}')"; 
    $create_post_query = mysqli_query($connection, $query); 
} 
?> 
<form action="fileup.php" method="post" enctype="multipart/form-data">  

     <label for="post_image">Post Image</label> 
     <input type="file" name="image"> 
     <input type="submit" name="create_post" value="Publish"> 

</form> 

VARCHAR(100)inoughです:任意およびすべてのヘルプは非常にこれは、アップロードのイメージのためのコードである

+0

ブロブタイプを使用して、一度mysqlデータベースに画像を保存しました。 –

+0

varcharは、イメージをBase64でエンコードされた文字列として保存する場合にのみ機能します。これはおそらく500文字を超える文字が必要になります。 –

答えて

0

いただければ幸いです。

関連する問題