无法将图像上传到mySQL数据库(JSON / Volley / PHP)

时间:2016-08-06 11:24:25

标签: php mysql blob android-volley

请帮助一个人..我很近,我能感受到它。如果重复但没有看到任何有效的答案,请原谅我。

我创建了一个mySQL数据库并已使用php / android / volley连接,并开始通过应用程序更新数据。所以我知道连接和添加数据位有效。

但是我现在需要将图像添加到数据库中。而且它没有发生。根本没有将新行添加到数据库中(在添加blob列之前它已经添加)

我一直在关注两个教程的混合,同时使用本地WAMP myphpsql

https://www.simplifiedcoding.net/android-volley-tutorial-to-upload-image-to-server/

https://www.simplifiedcoding.net/android-upload-image-to-server-using-php-mysql/。 (看看php代码)

我认为我的问题在于php方面。

SQL表结构 enter image description here

PHP:DB_Functions_FamilyMembers.php

 public function storeFamilyMember($name, $account_id, $bio, $user_pic) {

  $stmt = $this->conn->prepare("INSERT INTO family_member(name, account_id, bio, user_pic) VALUES(?,?,?,?)");
  $stmt->bind_param("ssss", $name, $account_id, $bio,  $user_pic);

    $result = $stmt->execute();

    $stmt->close();


    if ($result) {
        $stmt = $this->conn->prepare("SELECT * FROM family_member WHERE account_id = ?");
        $stmt->bind_param("s", $name);
        $stmt->execute();
        $family_member = $stmt->get_result()->fetch_assoc();
        $stmt->close();

        return $family_member;
    } else {
        return false;
    }

}

PHP:familymembers.php

<?php 

require_once 'include/DB_Functions_FamilyMembers.php';
$db = new DB_Functions_FamilyMembers();
// json response array
$response = array("error" => FALSE);

//if (isset($_POST['name'])) {
if (isset($_POST['name'], $_POST['account_id'], $_POST['bio']), $POST['user_pic']) {

    // receiving the post params
    $name = $_POST['name'];
    $account_id = $_POST['account_id'];
    $bio = $_POST['bio'];
    $user_pic = $_POST['user_pic'];

        $family_member = $db->storeFamilyMember($name, $account_id, $bio, $user_pic);

    $response["error"] = FALSE;
    $response["family_member"]["name"] = $family_member["name"];
    $response["family_member"]["account_id"] = $family_member["account_id"];
    $response["family_member"]["bio"] = $family_member["bio"];
    $response["family_member"]["user_pic"] = $family_member["user_pic"];
    echo json_encode($response);

    }else{
        $response["error"] = TRUE;
        $response["error_msg"] = "Unknown error occurred in registration!";

       echo json_encode($response);

    }
?>

ANDROID:FamilyMemberFragmentAdd.java - 排球地图

 @Override
            protected Map<String, String> getParams(){
                String uploadImage = getStringImage(bitmap);
                // Posting params to register url
                Map<String, String> params = new HashMap<String, String>();
                params.put("name", name);
                params.put("account_id", id);
                params.put("bio", bio);
                params.put("user_pic", uploadImage );
                return params;
            }

ANDROID:FamilyMemberFragmentAdd.java - METHOD:getStringImage(Bitmap bitmap)

  public String getStringImage(Bitmap bmp){

     ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        return encodedImage;
    }

我认为它位于PHP / SQL设置中,因为在我尝试上传blob之前所有其他字段都已更新。

谢谢!

1 个答案:

答案 0 :(得分:1)

我有一个示例代码,我上传图像并存储在数据库中,您可以检查。     

$mysql= mysqli_connect("localhost", "root", "", "test");
$response = array(); // response to client




    if(is_uploaded_file($_FILES["user_image"]["tmp_name"]) && @$_POST["user_name"]){
        $tmp_file = $_FILES["user_image"]["tmp_name"]; //get file from client
        $img_name = $_FILES["user_image"]["name"];
        $upload_dir = "./image/".$img_name;

        $sql = " INSERT INTO user (user_name,user_profile) VALUES ('{$_POST['user_name']}', '{$img_name}')";
        if (move_uploaded_file($tmp_file, $upload_dir) && $mysql->query($sql)){
            $response["MESSAGE"] = "UPLOAD SUCCED";
            $response["STATUS"] =200;
        }
        else{
            $response["MESSAGE"] = "UPLOAD FAILED";
            $response["STATUS"] = 404;
        }
    }else{

        $response["MESSAGE"] = "INVALID REQUEST";
        $response["STATUS"] =400;
    }

echo json_encode($response);
?>