使用php myql db将用户配置文件pic保存到db

时间:2017-03-12 18:44:08

标签: php mysql database

我对php& mysql,并想请求帮助将用户的个人资料图片保存到我的数据库。目前,一旦用户点击保存,这就是执行的代码

斯威夫特3:

   @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在WebService类中执行

     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脚本:

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),但我不确定。任何和所有的帮助将非常感激

1 个答案:

答案 0 :(得分:0)

这是上传图片的代码:

<?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)已经过了。