title: Upload Image to MySQL server from Android Studio
body: <p>I am trying to upload an image to a MySQL database and I am getting a message from PHP saying that the image has been uploaded, but when I log into my server the value is <code>NULL</code>. The Image is uploaded in my running app but its name or path cannot be added. Kindly, how can I solve this problem?</p>
  <pre><code>&lt;?php
      if($_SERVER['REQUEST_METHOD']=='POST') {
          $pet_image=$_POST['pet_image'];
          $sql = &quot;INSERT INTO pet_info (pet_image) VALUES (?)&quot;;
                  
          $stmt = mysqli_prepare($conn,$sql);
                  
          mysqli_stmt_bind_param($stmt,&quot;s&quot;,$pet_image);
          mysqli_stmt_execute($stmt);
                  
          $check = mysqli_stmt_affected_rows($stmt);
                  
          if($check!=0){
              echo &quot;Image Uploaded Successfully&quot;;
          } else {
              echo &quot;Error Uploading Image&quot;;
          }
      }
  ?&gt;
  </code></pre>
  <p>My Activity.java</p>
  <pre><code>    public class RequestHandler {
          public String sendGetRequest(String uri) {
              try {
                  URL url = new URL(uri);
                  HttpURLConnection con = (HttpURLConnection) url.openConnection();
                  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                  String result;
      
                  StringBuilder sb = new StringBuilder();   
                  while ((result = bufferedReader.readLine()) != null) {
                      sb.append(result);
                  }
                  return sb.toString();
              } catch (Exception e) {
                  return null;
              }
          }
      
          public String sendPostRequest(String requestURL,
                                        HashMap&lt;String, String&gt; postDataParams) {
              URL url;
              String response = &quot;&quot;;
              try {
                  url = new URL(requestURL);
      
                  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                  conn.setReadTimeout(15000);
                  conn.setConnectTimeout(15000);
                  conn.setRequestMethod(&quot;POST&quot;);
                  conn.setDoInput(true);
                  conn.setDoOutput(true);
      
      
                  OutputStream os = conn.getOutputStream();
                  BufferedWriter writer = new BufferedWriter(
                          new OutputStreamWriter(os, &quot;UTF-8&quot;));
                  writer.write(getPostDataString(postDataParams));
      
                  writer.flush();
                  writer.close();
                  os.close();
                  int responseCode = conn.getResponseCode();
      
                  if (responseCode == HttpsURLConnection.HTTP_OK) {
                     BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                      response = br.readLine();
                  } else {
                      response = &quot;Error Registering&quot;;
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
              return response;
          }
      
          private String getPostDataString(HashMap&lt;String, String&gt; params) throws UnsupportedEncodingException {
              StringBuilder result = new StringBuilder();
              boolean first = true;
              for (Map.Entry&lt;String, String&gt; entry : params.entrySet()) {
                  if (first)
                      first = false;
                  else
                      result.append(&quot;&amp;&quot;);
      
                  result.append(URLEncoder.encode(entry.getKey(), &quot;UTF-8&quot;));
                  result.append(&quot;=&quot;);
                  result.append(URLEncoder.encode(entry.getValue(), &quot;UTF-8&quot;));
              }
              return result.toString();
          }
              
          private void showFileChooser() {
              Intent intent = new Intent();
              intent.setType(&quot;image/*&quot;);
              intent.setAction(Intent.ACTION_GET_CONTENT);
              startActivityForResult(Intent.createChooser(intent, &quot;Select Picture&quot;), PICK_IMAGE_REQUEST);
          }
      
          @Override
          protected void onActivityResult(int requestCode, 
                                          int resultCode, Intent data) {
             super.onActivityResult(requestCode, resultCode, data);
      
              if (requestCode == PICK_IMAGE_REQUEST &amp;&amp; resultCode == RESULT_OK &amp;&amp; data != null &amp;&amp; data.getData() != null) {
                  Uri filePath = data.getData();
                  bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                  setToImageView(bitmap);
                  //pet_image.setImageBitmap(bitmap);
              }
          }
          
          private void setToImageView(Bitmap bmp) {
              //compress image
              ByteArrayOutputStream bytes = new ByteArrayOutputStream();
              bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
              Bitmap decoded = BitmapFactory.decodeStream(new 
                                   ByteArrayInputStream(bytes.toByteArray()));
              pet_image.setImageBitmap(decoded);
          }
         
          public String getStringImage(Bitmap bmp){
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
              byte[] imageBytes = baos.toByteArray();
              String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
              return encodedImage;
          }
  
          private void uploadImage() {
              class UploadImage extends AsyncTask&lt;Bitmap, Void, String&gt; {
      
                  ProgressDialog loading;
                  RequestHandler rh = new RequestHandler();
      
                  @Override
                  protected void onPreExecute() {
                      super.onPreExecute();
                      loading = ProgressDialog.show(My_pet.this, &quot;Saving....&quot;, &quot;Please wait...&quot;, true, true);
                  }
      
                  @Override
                  protected void onPostExecute(String s) {
                      super.onPostExecute(s);
                      loading.dismiss();
                      Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
                  }
      
                  @Override
                  protected String doInBackground(Bitmap... params) {
                      Bitmap bitmapp = params[0];
                      String uploadImage = getStringImage(bitmapp);
      
                      HashMap&lt;String, String&gt; data = new HashMap&lt;&gt;();
                      data.put(UPLOAD_KEY, uploadImage);
      
                      String result = rh.sendPostRequest(UPLOAD_URL, data);
      
                      return result;
                  }
              }
              UploadImage ui = new UploadImage();
              ui.execute(bitmap);
          }
     }
  </code></pre>