doqrjrc95405 2019-07-31 10:16
浏览 90

为什么我的参数没有输入数据库? (科特林-PHP)

I have a php server with a post method with 3 parameters where the first parameter is used for user_id, the second for the transaction id (trans_id) and the last for the photo file. as follows :

include_once "conn.php";
//Getting the server ip
$server_ip = gethostbyname(gethostname());
//creating the upload url
$upload_url = 'http://'.$server_ip.'/ecommerce/assets/images/user/';

//response array
$response = array();
  $username = $_POST['user'];
    $trans_id = $_POST['trans_id'];
  //getting file info from the request
  $fileinfo = pathinfo($_FILES['image']['name']);
  //getting the file extension
  $extension = '.'.$fileinfo['extension'];
  $file_url = $username.time();
  $file_path = 'assets/images/komplain/' . $file_url . $extension;
  $uploadPath = 'assets/images/komplain/';
  $result = array("success" => $_FILES["image"]["name"]);
  try{
                //saving the file$sourceProperties = getimagesize($fileName);
          $fileName = $_FILES['image']['tmp_name'];
          $sourceProperties = getimagesize($fileName);
           $fileExt = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
          $uploadImageType = $sourceProperties[2];
          $sourceImageWidth = $sourceProperties[0];
          $sourceImageHeight = $sourceProperties[1];
          switch ($uploadImageType) {
             case IMAGETYPE_JPEG:
                 $resourceType = imagecreatefromjpeg($fileName);
                 $imageLayer = resizeImage($resourceType,$sourceImageWidth,$sourceImageHeight);
                 imagejpeg($imageLayer,$uploadPath.$file_url."_thumb".'.'. $fileExt);
                 break;

             case IMAGETYPE_GIF:
                 $resourceType = imagecreatefromgif($fileName);
                 $imageLayer = resizeImage($resourceType,$sourceImageWidth,$sourceImageHeight);
                 imagegif($imageLayer,$uploadPath.$file_url."_thumb".'.'. $fileExt);
                 break;

             case IMAGETYPE_PNG:
                 $resourceType = imagecreatefrompng($fileName);
                 $imageLayer = resizeImage($resourceType,$sourceImageWidth,$sourceImageHeight);
                 imagepng($imageLayer,$uploadPath.$file_url."_thumb".'.'. $fileExt);
                 break;

             default:
                 $imageProcess = 0;
                 break;
         }

          // File successfully uploaded
                //adding the path and name to database
                if(move_uploaded_file($_FILES['image']['tmp_name'],$file_path)){
            $sql =  "UPDATE komplain SET foto='$file_url', foto_type='$extension' WHERE user = '$username' AND trans_id=$trans_id";
            mysqli_query($conn,$sql);
            $response['message'] = 'File uploaded successfully!';
                $response['status'] = 'success';
                $response['file_path'] = $upload_url."/ecommerce". $file_url . $extension;
                }
            //if some error occurred
  }catch(Exception $e){
          $response['status'] = 'fail';
              $response['message'] = $e->getMessage();
  }
echo mysqli_error($conn);
echo json_encode($response, JSON_PRETTY_PRINT);

function resizeImage($resourceType,$image_width,$image_height) {
    $resizeWidth = 250;
    $resizeHeight = 250;
    $imageLayer = imagecreatetruecolor($resizeWidth,$resizeHeight);
    imagecopyresampled($imageLayer,$resourceType,0,0,0,0,$resizeWidth,$resizeHeight, $image_width,$image_height);
    return $imageLayer;
}

Then I use kotlin as a client and use the MultipartUploadRequest method to upload to the server. as follows :

btn_send.setOnClickListener{  uploadMultipart() }

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

        if (requestCode == IMAGE_REQUEST_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
            uri = data.getData();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri)
                img.setImageBitmap(bitmap)
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }

    }

    fun getPath(uri: Uri): String {
        var cursor = getContentResolver().query(uri, null, null, null, null)
        cursor.moveToFirst()
        var document_id = cursor.getString(0)
        document_id = document_id.substring(document_id.lastIndexOf(":") + 1)
        cursor.close()

        cursor = getContentResolver().query(
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            null,
            MediaStore.Images.Media._ID + " = ? ",
            arrayOf<String>(document_id),
            null
        )
        cursor.moveToFirst()
        val path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA))
        cursor.close()

        return path
    }

    fun uploadMultipart() {
        //getting the actual path of the image
        val path = getPath(uri!!)
        //Uploading code
        try {
            val uploadId = UUID.randomUUID().toString()

            //Creating a multi part request
            MultipartUploadRequest(this, uploadId, UPLOAD_URL)
                .addFileToUpload(path, "image") //Adding file
                .addParameter("user", MCart.user_id) //Adding text parameter to the request
                .addParameter("trans_id", trans.toString()) //Adding text parameter to the request
                .setNotificationConfig(UploadNotificationConfig())
                .setMaxRetries(2)
                .startUpload() //Starting the upload
        } catch (exc: Exception) {
            Toast.makeText(this, exc.message, Toast.LENGTH_SHORT).show()
        }

    }

The problem that I get is, I only get the image file that was successfully uploaded in the server folder without any records in my mysql. I have entered trans_id and user_id that I have created in (.addParameter) but I do not get anything in mysql (database)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)