how to upload more than one image to server(mysql database) using php and android

允我心安 提交于 2019-12-01 12:31:26
meda

Please refer to my answer to the question:

Save multiple image into mysql php from android but only one image get inserted

Edit:

In your PHP sript you are overwriting the image upload because you are using the same upload path for both images.

You must make sure the $path value is unique .

Try this script:

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

    define('HOST','hostname');
    define('USER','username');
    define('PASS','password');
    define('DB','dbname');

    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

    $path = "uploads/".uniqid().".png";
    $path1 = "uploads/".uniqid().".png";

    $actualpath = "http://oursite/PhotoUploadWithText/$path";
    $actualpath1 = "http://oursite/PhotoUploadWithText/$path1";

    $sql = "INSERT INTO uploads (image,image1,name) VALUES ('$actualpath','$actualpath1','$name')";

    if(mysqli_query($con,$sql)){
        file_put_contents($path,base64_decode($image));
        file_put_contents($path1,base64_decode($image1));
        echo "Successfully Uploaded";
    }

    mysqli_close($con);

}else{
    echo "Error";
}

You can use $id = uniqid(); in order to get different id for the images.

In your java code, change private int PICK_IMAGE_REQUEST1 = 1; to private int PICK_IMAGE_REQUEST1 = 2;

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case PICK_IMAGE_REQUEST:
                if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK & null != data) {
                   Uri filePath = data.getData();
    try {
        bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
        //bitmap1 = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
        imageView.setImageBitmap(bitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }
                }

                break;

            case PICK_IMAGE_REQUEST1:
                if (requestCode == PICK_IMAGE_REQUEST1 && resultCode == RESULT_OK) {
                    Uri filePath = data.getData();
    try {
        //bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
        bitmap1 = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
        //imageView.setImageBitmap(bitmap);
        imageView1.setImageBitmap(bitmap1);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!