I'm having issues when trying to send a Base64 encoded bitmap through a HttpURLConnection to a POST variable on my PHP script.
It displays the toast "Upload Successful" but my database is not populating.
I first convert my Bitmap variable to Base64, where bSignature is the Bitmap and sSignature is the String.
sSignature = encodeToBase64(bSignature, Bitmap.CompressFormat.JPEG,100);
Here is my AsyncTask code:
class BackgroundTaskSig extends AsyncTask<String,Void,String> {
String json_signature_url;
@Override
protected void onPreExecute() {
json_signature_url = "http://abhandyman.x10host.com/upload_signature.php";
}
@Override
protected String doInBackground(String... args) {
sSignature = args[0];
try {
URL url = new URL(json_signature_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data_string = URLEncoder.encode("image","UTF-8")+"="+URLEncoder.encode(sSignature,"UTF-8");
bufferedWriter.write(data_string);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
inputStream.close();
httpURLConnection.disconnect();
return "Upload Successful";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
}
}
Here is my PHP script, where the structure signature column is set to 'blob'
<?php
require "init_signature.php";
$job_id = "1";
$image = $_POST['image'];
$image = base64_decode($image);
$sql_query = "UPDATE signature SET signature='$image' WHERE id='$job_id'";
if (mysqli_query($con,$sql_query)){
echo "Image uploaded successfully";
}else{
echo "Error in insertion " . mysqli_error($con);
}
?>
I have a feeling its something in my PHP script that's incorrect. Any second opinion would be much appreciated!
Thanks in advance
EDIT: So i've resolved the T_VARIABLE error message (Small syntax problem) - now i've got this error message: