【问题标题】:Undefined index image in phpphp中未定义的索引图像
【发布时间】:2016-04-16 13:28:15
【问题描述】:

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

我想将图像路径从android 存储到MySQL 并将图像存储到 PhotoUpload 目录中。

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

活动 A

 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();
        }
    }
?>

这是我的预期

错误

未定义索引:C 中的图像:......保存图像时出错

这是第 10 行 $image = $_POST['image'];

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

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

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

【问题讨论】:

  • $_POST['image'] => $_FILES['image']
  • 翻译 Fred ii 所说的:上传在 $_FILES 数组中,而不是在 $_POST 中。
  • @Fred-ii- 试过了,没有运气
  • @devlincarnate 不太明白
  • 处理文件时还需要有效的enctype

标签: php android mysql image


【解决方案1】:

既然你在这个$image = $_POST['image']; 中没有定义,为什么不在你的doInBackground 中添加image?

试试这个

  @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;
            }

【讨论】:

    猜你喜欢
    • 2014-05-17
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    相关资源
    最近更新 更多