0

我想在 Android Mobile Programming 上将图像文件上传到 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)中,任何人都可以帮助我吗?谢谢之前

最好的祝福

4

2 回答 2

1

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

$sql = "UPDATE login SET file = '$path' WHERE username_user = '$username'";
$r = mysql_query($sql);
于 2012-06-28T08:59:44.763 回答
0

这条线

$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); 
于 2012-06-28T08:56:38.940 回答