gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:21:43 25 4
gpt4 key购买 nike

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

我已经创建了一个 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 - Volley map

 @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 位图)

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

关于php - 无法将图像上传到 mySQL 数据库 (JSON/Volley/PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38803729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com