title: Get trouble when want to store image path into MySQL
body: <p>In my Activity A, it has a listView where the image and text were get from Activity B.</p>
  
  <p><strong>Activtiy A listView with image and text</strong></p>
  
  <p><a href="https://i.stack.imgur.com/ImO0z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ImO0z.png" alt="enter image description here"></a></p>
  
  <p>When the save button is clicked, I want to store image path and text into mysql through php, and image will stored in directory.</p>
  
  <p><strong>Activiy A</strong></p>
  
  <pre><code>  public void uploadImageAndText(ArrayList&lt;ImageAndText&gt; listItems, final String id) {
              JSONArray jsonArray = new JSONArray();
              try {
                  for (ImageAndText i : listItems) {
                      JSONObject object = new JSONObject();
                      String type = i.getType();
                      String[] Type = type.split(":");
                      object.put("type", Type[1]);
                      Toast.makeText(getApplicationContext(), Type[1], Toast.LENGTH_LONG).show();
                      String amount = i.getAmount();
                      String[] Amount = amount.split(":");
                      object.put("amount", Amount[1]);
                      String description = i.getDescription();
                      String[] Description = description.split(":");
                      object.put("description", Description[1]);
                      Bitmap uploadImage = i.getImage();
                      String image = getStringImage(uploadImage);
                      object.put("image", image);
                      object.put("ts_id", id);
                      jsonArray.put(object);
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
  
              AddStaff ru = new AddStaff(jsonArray);
              ru.execute();
  
          }
  
          class AddStaff extends AsyncTask&lt;String, Void, String&gt; {
              ProgressDialog loading;
  
              JSONArray jsonArray;
  
              AddStaff(JSONArray jsonArray) {
                  this.jsonArray = jsonArray;
              }
  
              @Override
              protected void onPreExecute() {
                  super.onPreExecute();
                  loading = ProgressDialog.show(AddClaims.this, "Please Wait", null, true, true);
              }
  
              @Override
              protected String doInBackground(String... params) {
                  HashMap&lt;String, String&gt; data = new HashMap&lt;String, String&gt;();
                  data.put("listItems", jsonArray.toString());
                  RequestHandler rh = new RequestHandler();
                  String result = rh.sendPostRequest(Configs.STAFF_BENEFIT, data);
                  return result;
              }
  
              @Override
              protected void onPostExecute(String s) {
                  super.onPostExecute(s);
                  loading.dismiss();
                  Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
              }
          }
  
  
          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;
          }
      }
  </code></pre>
  
  <p><strong>php</strong></p>
  
  <pre><code>  &lt;?php
      if( $_SERVER['REQUEST_METHOD']=='POST' ){
  
          if( !empty( $_POST['listItems'] ) ){
  
              $mysqli = new mysqli("127.0.0.1:3307", "root", "", "androiddb");
              if( $mysqli-&gt;connect_errno ) echo "Failed to connect to MySQL";/* try not to reveal too much info! */
  
              /* The example you followed had this line! */
              $image = $_POST['image'];
  
              $listItems = json_decode( $_POST['listItems'], true ); 
  
              /* This never gets called - should it? */
              $sql="SELECT id FROM staff_benefit ORDER BY id ASC";
              /*
                  This fetches ALL ids from the staff_benefit table
                  so which ID do you need? Iterating through the 
                  recordset will effectively return the last one!
              */
  
              $id=0;
  
  
  
              /*
                  I assume that $id is supposed to get 
                  the value from th above sql query???
              */      
              $res=$mysqli-&gt;query( $sql );
              while( $rs=$res-&gt;fetch_object() ) $id=$rs-&gt;id;
  
              $path="$id.png";
              $actualpath="http://192.168.107.115:80/Android/CRUD/PhotoUpload/$path";
  
  
              /* Use only placeholders in your prepared statement */
              $sql="INSERT INTO `staff_benefit` ( `type`, `amount`, `description`, `image`, `ts_id` ) VALUES ( ?, ?, ?, ?, ? )";
              $stmt=$mysqli-&gt;prepare( $sql );
  
  
              /* Save the image to disk: I prefer to use the actual path for storing files */
              $pathelements=array( realpath( $_SERVER['DOCUMENT_ROOT'] ), 'CRUD', 'PhotoUpload', '' );/* empty entry adds trailing slash to path */
              $savepath = realpath( implode( DIRECTORY_SEPARATOR, $pathelements ) ) . "{$id}.png";
  
              $bytes=file_put_contents( $savepath, base64_decode( $image ) );
              if( !$bytes ){
                  echo 'Error saving image';  
              }
  
              if ( $stmt &amp;&amp; $bytes ) {
                   foreach( $listItems as $item ){ 
  
                      $stmt-&gt;bind_param('sssss', $item['type'], $item['amount'], $item['description'], $actualpath, $item['ts_id'] );
                      $res=$stmt-&gt;execute();
  
                      if( !$res ) echo 'Query failed with code: '.$stmt-&gt;errno;/* Again, not too much info */
                  } 
              }
              $mysqli-&gt;close();
          }
      }
  ?&gt;
  </code></pre>
  
  <blockquote>
    <p>When click image in directory, can't display image because the file is
    empty</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/IhF5m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IhF5m.png" alt="enter image description here"></a></p>