行无法更新

时间:2016-01-18 03:50:30

标签: java php android mysql

有人可以帮帮我???

我已成功将图片路径和文字存储到MySQL,并将图像存储在文件夹中。

这是我用来上传图片路径和文字的php代码。

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

但是当我尝试更新android中的行时,该行没有改变,但它在php中显示updated successfully

这是我的更新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']))
        {

            $id = uniqid();
            $url="http://192.168.107.115: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_url' 
            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);
    }
?>

更新代码

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

   public void updateWithoutImage( final String claimType,  final String Amount, final String Description)
    {
        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("iD",ID);
                    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);
                Log.e("Type",claimType);
                hashMap.put(Configs.KEY_AMOUNT, Amount);
                hashMap.put(Configs.KEY_DESCRIPTION, Description);
                RequestHandler rh = new RequestHandler();
                String s = rh.sendPostRequest(Configs.URL_UPDATEDE_IMAGE_TEXT,hashMap);
                return s;
            }
        }

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

我有两个功能,一个是更新,另一个是 updateWithoutImage 。他们使用相同的PHP。但php适用于 updateWithoutImage ,但不适用于更新功能

1 个答案:

答案 0 :(得分:0)

好的,我知道为什么update()不起作用。

在我的PHP代码中,我有两个$id。将$id内的if(isset($_POST['image']))更改为$id1,现在可以更新。