php - php中 undefined index 图像

标签 php android mysql image

首先,让我描述一下我在这里面临的问题。

我想存储从 androidMySQL 的图像路径,并将图像存储到 PhotoUpload 目录中。

当提交按钮被点击时,所有的listItem将被保存到MySQL

Activity A

enter image description here

 public void uploadImageAndText(ArrayList<ImageAndText> 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<String, Void, String> {
            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<String, String> data = new HashMap<String, String>();
                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;
        }
    }

php

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

        if( !empty( $_POST['listItems'] ) ){

            $mysqli = new mysqli("127.0.0.1:3307", "root", "", "androiddb");
            if( $mysqli->connect_errno ) echo "Failed to connect to MySQL";

            $image = $_POST['image'];

            $listItems = json_decode( $_POST['listItems'], true ); 

            $sql="SELECT id FROM staff_benefit ORDER BY id ASC";

            $id=0;

            $res=$mysqli->query( $sql );
            while( $rs=$res->fetch_object() ) $id=$rs->id;

            $path="$id.png";
            $actualpath="http://192.168.107.115:80/Android/CRUD/PhotoUpload/$path";

            $sql="INSERT INTO `staff_benefit` ( `type`, `amount`, `description`, `image`, `ts_id` ) VALUES ( ?, ?, ?, ?, ? )";
            $stmt=$mysqli->prepare( $sql );

            $pathelements=array( realpath( $_SERVER['DOCUMENT_ROOT'] ), 'CRUD', 'PhotoUpload', '' );
            $savepath = realpath( implode( DIRECTORY_SEPARATOR, $pathelements ) ) . "{$id}.png";

            $bytes=file_put_contents( $savepath, base64_decode( $image ) );
            if( !$bytes ){
                echo 'Error saving image';  
            }

            if ( $stmt && $bytes ) {
                 foreach( $listItems as $item ){ 

                    $stmt->bind_param('sssss', $item['type'], $item['amount'], $item['description'], $actualpath, $item['ts_id'] );
                    $res=$stmt->execute();

                    if( !$res ) echo 'Query failed with code: '.$stmt->errno;
                } 
            }
            $mysqli->close();
        }
    }
?>

这是我所期待的

enter image description here

错误

Undefined index: image in C:...... Error saving images

This is line 10 $image = $_POST['image'];

我知道我收到此错误消息是因为我只发布了 listItem 而没有发布图像。 我的问题是如何分解 listItem 中的图像然后对其进行解码?

有人可以帮助我如何将数组列表(带有图像)存储到 MySQL 中吗?非常感谢

我关注这个tutorial ,但问题是他存储的是单个文件,而不是 arrayList!

最佳答案

由于您在此 $image = $_POST['image']; 中未定义,为什么不添加 image在你的doInBackground

试试这个

  @Override
            protected String doInBackground(String... params) {
                HashMap<String, String> data = new HashMap<String, String>();
                data.put("listItems", jsonArray.toString());
                data.put(Configs.KEY_IMAGE,imagess);
                RequestHandler rh = new RequestHandler();
                String result = rh.sendPostRequest(Configs.STAFF_BENEFIT, data);
                return result;
            }

关于php - php中 undefined index 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34735076/

相关文章:

php - 如果名称使用其他语言或特殊字符,为什么图像不显示?

php - 将数据从一个表复制到另一个没有 ID

php - SQLSTATE[HY000] [1044] cPanel 中的用户访问被拒绝

php - 使用 Eloquent 模型类从数据库中选择记录时,如何在 Laravel 4 中解密密码?

Android:如何将RelativeLayout填充到屏幕的全宽?

Android:如何设置编译后的APK名称?

SQL查询错误: Operand should contain 1 column(s) in rails

php - 无法在一条语句中执行多个更新查询

PHP - Symfony2 功能测试 HTTPS 路由

java - LibGDX - 无法从 FrameBuffer 获取纹理