Php Android:将图像上传到mysql
我想在Android移动编程平台上上传一个图像文件到mysql数据库,但我遇到了一些问题。这是我的密码: 我在Eclipse上的代码:Php Android:将图像上传到mysql,php,android,mysql,database,file-upload,Php,Android,Mysql,Database,File Upload,我想在Android移动编程平台上上传一个图像文件到mysql数据库,但我遇到了一些问题。这是我的密码: 我在Eclipse上的代码: private void uploadFile() { // TODO Auto-generated method stub String nama = getIntent().getStringExtra("user"); Bundle fieldresults = this.getIntent().getExtras(); F
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);
?>
这行
$sql = "UPDATE login SET file = '" . $path . "' Where username_user = $username; $r=mysql_query($sql);
应该读
$sql = "UPDATE login SET file = '" . $path . "' Where username_user = " . $username; $r=mysql_query($sql);
您的$sql出现错误,请尝试:
$sql = "UPDATE login SET file = '$path' WHERE username_user = '$username'";
$r = mysql_query($sql);
$\u请求['filename']不是失败的原因,但此处的小写字母为e。它应该是$_REQUEST['filename']:-)还有您遇到了什么错误?你确定你有db连接吗?另外,我看不到您在哪里执行sql?我已经尝试过了,但是图像仍然无法显示,我想知道我的代码中是否遗漏了一些内容,顺便说一句,文件在设备中,pict库(仿真器)没有错误,只是文件没有显示在mysql数据库上,也只是为了让你知道mysql_uu函数现在都被删除了,我更改了代码,但结果还是一样的,顺便说一句,在mysql中,我把文件放在LONGBLOB类型,我想知道错误是否在我的android代码上?你需要在做任何事情之前弄清楚sql查询是否正在执行,当我检查文件夹“img”时,文件已上载到那里,现在的问题是数据库未更新,是否缺少某些内容(它仍然不起作用,嗯,我想知道我的android代码中是否缺少一些东西?