簡體   English   中英

無法將圖像上傳到mySQL數據庫(JSON / Volley / PHP)

[英]Cannot upload an Image to mySQL database (JSON/Volley/PHP)

請幫助一個人..我很親密,我能感覺到。 如果有重復,請原諒我,但看不到任何有效的答案。

我已經創建了一個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表結構 在此處輸入圖片說明

的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-方法: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之前,所有其他字段均已更新。

謝謝!

我有一個示例代碼,我在其中上傳圖像並將其存儲在數據庫中,您可以進行檢查。

$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);
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM