在PHP中未定义的索引图像 [英] Undefined index image in php

查看:134
本文介绍了在PHP中未定义的索引图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Firstly,let me describe what are the issue I facing here.

我想从存储 图像路径的Andr​​oid为的MySQL 和存储图像到PhotoUpload目录。

I'm wanted to store the image path from android to MySQL and store the image into PhotoUpload directory.

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

When the submit button is clicked, all the listItem will be saved into 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图像:......错误保存图像

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

这是10号线 $图像= $ _ POST ['形象'];

我知道我得到这个错误信息是因为我只发布的listItem ,但没有图像。 我的问题是我怎么能打破的listItem的图像,然后去code呢?

I know I'm getting this error message is because I only post listItem but no image. My problem is how can I break the image in listItem and then decode it ?

有人请帮助我如何ArrayList的存储(含图片)到MySQL?多谢

Can someone please assist me on how to store arraylist(with image) into MySQL ?Thanks a lot

我按照这个教程,但问题是,他存储的单个文件,ArrayList不是!

I'm follow this tutorial, but the problem is he storing a single file, not arrayList!

推荐答案

既然你在这个不确定的 $图像= $ _ POST ['形象']; ,你为什么不添加图片你的 doInBackground

Since you get undefined in this $image = $_POST['image'];, why you don't add image in your 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中未定义的索引图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆