【发布时间】:2016-08-06 11:24:24
【问题描述】:
请帮助一个人。我很接近我能感觉到。如果重复但没有看到任何有效的答案,请原谅我。
我已经创建了一个 mySQL 数据库,并且已经使用 php/android/volley 连接并开始通过应用程序更新数据。所以我知道连接和添加数据位是有效的。
但是我现在需要将图像添加到数据库中。它没有发生。一个新行根本没有添加到数据库中(在我添加 blob 列之前它已经添加)
我一直在学习两个教程的混合,同时使用本地 WAMP mysqlsql
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 方面。
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 之前,所有其他字段都已正常更新。
谢谢!
【问题讨论】:
-
dev.mysql.com/doc/refman/5.7/en/… 对于 Web 服务器,将图像和其他二进制资产存储为文件,路径名存储在数据库中,而不是文件本身。大多数 Web 服务器比数据库内容更擅长缓存文件,因此使用文件通常更快。 (尽管在这种情况下您必须自己处理备份和存储问题。)
-
好吧,这很有意义。因此,对于我的应用程序,由于用户要么使用相机,要么从图库中挑选照片,我认为我需要以某种方式将该照片上传到服务器(这对我来说是本地的)。然后我假设我需要使用 php 文件来加载该路径,然后将其应用于 mySQL 列。这是正确的方法吗?感谢您的宝贵时间
-
对,没错,只保存文件路径。因此,您的服务器端代码或多或少类似于 php 文档中的文件上传示例代码。
-
是否因为重复而被否决?或者问题结构,如果重复我删除问题,如果问题结构我将重新构建它。
-
荣誉归@e4c5 :)
标签: php mysql blob android-volley