doujiaoben28596 2016-01-17 11:17
浏览 79
已采纳

更新MySQL php中的图像路径

Can someone please help me ? How can I update an image path ?

I have successfully store the image path and text into MySQL, and image in the folder.

This is the php code I use to upload image path and text.

enter image description here

 <?php
        if( $_SERVER['REQUEST_METHOD']=='POST' ){
            if( !empty( $_POST['listItems'] ) ){
                $listItems = json_decode( $_POST['listItems'], true ); 
                $mysqli = new mysqli("127.0.0.1:3307", "root", "", "androiddb");
                if( $mysqli->connect_errno ) echo "Failed to connect to MySQL";
                $sql="INSERT INTO `staff_benefit` 
                     ( `type`, `amount`, `description`, `image`, `ts_id` ) 
                      VALUES ( ?, ?, ?, ?, ? )";
                if($stmt=$mysqli->prepare($sql )){
                    $url="http://192.168.1.7:80/Android/CRUD/PhotoUpload/";
                    foreach( $listItems as $item ){ 
                        $id = uniqid();
                        $image_name = $id.".png";
                        $save_path = 'PhotoUpload/'.$image_name;
                        $image_url = $url.$image_name;
                        $bytes=file_put_contents($save_path, base64_decode($item['image']));
                        if( !$bytes ){
                            echo 'Error saving image';  
                        }else{
                            $stmt->bind_param('sssss', 
                            $item['type'], 
                            $item['amount'], 
                            $item['description'], 
                            $image_url, 
                            $item['ts_id'] );
                            if( !$res=$stmt->execute()){ 
                                echo 'Query failed with code: '.$stmt->errno;
                            }
                        }
                    } 
                }
                $mysqli->close();
            }
        }
    ?>

But when I tried to update the row in android, the image path will change from http:// to /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQ.. as image below.

enter image description here

This is my update php

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

        $id = $_POST['id'];
        $type = $_POST['type'];
        $amount = $_POST['amount'];
        $description = $_POST['description'];

        //importing database connection script 
        require_once('dbConnect.php');

        if(isset($_POST['image']))
        {
            $image=$_POST['image'];
            $id = uniqid();
            $url="http://192.168.1.7:80/Android/CRUD/PhotoUpload/";
            $image_name = $id.".png";
            $save_path = 'PhotoUpload/'.$image_name;
            $image_url = $url.$image_name;
            $bytes =file_put_contents($save_path, base64_decode($_POST['image']));
              $sql = "UPDATE staff_benefit SET type = '$type', amount = '$amount', description='$description', image='$image' 
                WHERE id = '$id'";
        }
        else{

             $sql = "UPDATE staff_benefit SET type = '$type', amount = '$amount', description='$description'
                  WHERE id = '$id'";

        }

        //Updating database table 
        if(mysqli_query($con,$sql)){
            echo ' Updated Successfully';
        }else{

            echo mysqli_error($con);
            exit;
        }

        //closing connection 
        mysqli_close($con);
    }
?>

Update code

     public void update( final String claimType,  final String Amount, final String Description, final Uri imageUri)
        {
               class updateImageAndText extends AsyncTask<Void,Void,String>{
                  // ProgressDialog loading;
                   @Override
                   protected void onPreExecute() {
                       super.onPreExecute();
                      // loading = ProgressDialog.show(Edit_Staff.this,"Updating...","Wait...",false,false);
                   }

                   @Override
                   protected void onPostExecute(String s) {
                       super.onPostExecute(s);
                      // loading.dismiss();
                       Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
                       try {
                           Intent returnIntent = new Intent();
                           returnIntent.putExtra("ClaimType", claimType);
                           returnIntent.putExtra("Amount", Amount);
                           returnIntent.putExtra("Description", Description);
                           returnIntent.putExtra("photo", imageUri.toString());
                           setResult(Activity.RESULT_OK, returnIntent);
                           finish();
                       }catch(Exception e)
                       {

                       }
                   }

                   @Override
                   protected String doInBackground(Void... params) {
                       HashMap<String,String> hashMap = new HashMap<>();
                       hashMap.put(Configs.KEY_ID, String.valueOf(ID));
                       Log.e("ID", ID + "");
                       hashMap.put(Configs.KEY_TYPE, claimType);
                       hashMap.put(Configs.KEY_AMOUNT, Amount);
                       hashMap.put(Configs.KEY_DESCRIPTION, Description);
                       if(imageUri != null){
                           Log.d("log", "photo " + imageUri);
                           hashMap.put(Configs.KEY_IMAGE,getStringImage(imageUri));
                       }else{
                           Log.d("log", "photo is null " );
                       }
                       RequestHandler rh = new RequestHandler();
                       String s = rh.sendPostRequest(Configs.URL_UPDATEDE_IMAGE_TEXT,hashMap);
                       return s;
                   }
               }

            updateImageAndText ue = new updateImageAndText();
            ue.execute();
        }


  public String getStringImage(Uri imgUri) {

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imgUri);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] imageBytes = baos.toByteArray();
            String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
            return encodedImage;
        } catch (Exception e) {
        }

        return "";
    }

How can I update the image path ? Thanks a lot !

Error

enter image description here

  • 写回答

1条回答 默认 最新

  • duanji5116 2016-01-17 11:49
    关注

    In case of image upload you need to then properly save the image and create image path as you were doing in the previous script like this

    $id = uniqid();
            $url="http://192.168.1.7:80/Android/CRUD/PhotoUpload/";
            $image_name = $id.".png";
            $save_path = 'PhotoUpload/'.$image_name;
            $image_url = $url.$image_name;
            $bytes =file_put_contents($save_path, base64_decode($_POST['image']));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算