0

I want to upload an image file into mysql database on Android Mobile Programming, but I've some problem with it. Here's my code:

My code on 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());
    }
}

and my PHP code:

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

?>

The problem is the file didn't show in my database (mysql), anyone can help me? thanks before

best regards

4
  • $_ReQUEST['filename'] not a reason to fail but you have a lower case e here. It should be $_REQUEST['filename'] :-) Commented Jun 28, 2012 at 8:53
  • 1
    also what errors are you getting? Are you certain you have a db connection? also i cant see where you are executing your sql? Commented Jun 28, 2012 at 8:55
  • I've tried it, but the image still dind't show, i wonder if i've been missing somethings on my code, btw the file is in the device, pict library (emulator) Commented Jun 28, 2012 at 8:58
  • there's no error, it just that the file didn't show on mysql database Commented Jun 28, 2012 at 8:59

2 Answers 2

1

Your $sql looks wrong, try:

$sql = "UPDATE login SET file = '$path' WHERE username_user = '$username'";
$r = mysql_query($sql);
Sign up to request clarification or add additional context in comments.

4 Comments

also just so you know the mysql_ functions are all now depricated php.net/manual/en/mysqlinfo.api.choosing.php
I've change the code, but the result is still the same, btw in mysql i put file as LONGBLOB type, i wonder if the error is on my android code?
you need to work out if the sql query is even executing before anything
well, when i check on my foler "img" the file has been uploaded there, and the problem now is the database is not updated, is there something missing? :(
0

this line

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

should read

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

1 Comment

it still didn't work, hmmm, i wonder is there's something missing on my android code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.