gpt4 book ai didi

php - 安卓 : Upload Image to mysql

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

我想在 Android 移动编程上将图像文件上传到 mysql 数据库,但遇到一些问题。这是我的代码:

我在 Eclipse 上的代码:

private void uploadFile() {
// TODO Auto-generated method stub
String nama = getIntent().getStringExtra("user");
Bundle fieldresults = this.getIntent().getExtras();
Filepath = fieldresults.getString("bitmap");

Bitmap bitmapOrg= BitmapFactory.decodeFile(Filepath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
ArrayList nameValuePairs = new
ArrayList();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
nameValuePairs.add(new BasicNameValuePair("filename",Filepath));
nameValuePairs.add(new BasicNameValuePair("username_user",nama));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://10.0.2.2/BloodGlucose/uploadImage.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}

和我的 PHP 代码:

    <?php
include_once("koneksi.php");

$username = $_REQUEST['username_user'];


$hasil = mysql_query("select * from login");

$base = $_REQUEST['image'];
$filename = $_ReQUEST['filename']
$buffer=base64_decode($base);
$path = "img/".$filename.".jpg";
$handle = fopen($path, 'wb');
$numbytes = fwrite($handle, $buffer);
fclose($handle);
$conn=mysql_connect("localhost","root","");
mysql_select_db("db_bloodglucose");

$sql = "UPDATE login SET file = '" . $path . "' Where username_user = $username;
$r=mysql_query($sql);

?>

问题是该文件没有显示在我的数据库(mysql)中,任何人都可以帮助我吗?先谢谢了

最诚挚的问候

最佳答案

你的$sql看起来不对,试试:

$sql = "UPDATE login SET file = '$path' WHERE username_user = '$username'";
$r = mysql_query($sql);

关于php - 安卓 : Upload Image to mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11241027/

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