dongye9228 2019-06-20 07:58
浏览 81
已采纳

邮差请求返回null [重复]

I'm making an android application that can upload multiple image to server, to make sure that my php is working, i'm trying it on postman, but when i try it on postman the response is null

this is the reponse i get from postman

this is the body

this is the result i get

i've reading some website for tutorial of this app and write the php exactly like that but it's keep sending null on my post request

Android Upload Image using Android Upload Service

Android Upload Image From Gallery To Server Using PHP MySQL

Android Upload Image to Server Using PHP MySQL

Android Upload Image to Server Using PHP and MySQL

This is my php

<?php 
//importing dbDetails file 
    require_once 'dbDetails.php';

    //this is our upload folder 
    $upload_path = 'upload/';

    //Getting the server ip 
    $server_ip = gethostbyname(gethostname());

    //creating the upload url 
    $upload_url = 'http://'.$server_ip.'/opr57/'.$upload_path; 

    //response array 
    $response = array(); 


    if($_SERVER['REQUEST_METHOD']=='POST'){

        //checking the required parameters from the request 
        if(isset($_POST['nama']) and isset($_FILES['image']['foto_ktp'])){

            //connecting to the database 
            $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect...');

            //getting name from the request 
            $id_dropping = $_POST['id_dropping'];
            $nama = $_POST['nama'];
            $alamat = $_POST['alamat'];
            $tr = $_POST['tr'];
            $parkiran = $_POST['parkiran'];
            $resort = $_POST['resort'];
            $pinjaman = $_POST['pinjaman'];
            $latitude = $_POST['latitude'];
            $longitude = $_POST['longitude'];
            $ktp = $_POST['foto_ktp'];
            $nasabah = $_POST['foto_nasabah'];
            $status = $_POST['status'];
            $ktpinfo = pathinfo($_FILES['image']['foto_ktp']);
            $nasabahinfo = pathinfo($_FILES['image']['foto_nasabah']);
            $extensionktp = $ktpinfo['extension'];
            $extensionnasabah = $nasabahinfo['extension'];
            $ktpurl = $upload_url.getFileName().'.'.$extensionktp;
            $ktppath = 
$upload_path.getFileName().'.'.$extensionktp;
            $nasabahurl = $upload_url.getFileName().'.'.$extensionnasabah;
            $nasabahpath = $upload_path.getFileName().'.'.$extensionnasabah;



            //trying to save the file in the directory 
            try{
                //saving the file 
move_uploaded_file($_FILES['image']['tmp_nama'],$ktppath);
move_uploaded_file($_FILES['image']['tmp_nama'],$nasabahpath);
            try{
            //saving the file 
            move_uploaded_file($_FILES['image']['tmp_name'],$file_path);
            move_uploaded_file($_FILES['image2']['tmp_name'],$file_path2);
            $stmt = $conn->prepare("INSERT INTO dropping(id_dropping, nama, alamat, tr, parkiran, resort , pinjaman ,latitude,longitude,foto_ktp,foto_nasabah status) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)");
            $stmt->bind_param("ssssssssssss", $id_dropping,$name,$alamat,$tr,$parkiran,$resort,$pinjaman,$latitude,$longitude,$file_url,$file_url2,$status);
            //$sql = "INSERT INTO `opr57`.`dropping` (`id_dropping`,`nama`,`alamat`,`tr`,`parkiran`,`resort`,`pinjaman`,`latitude`,`longitude`,`foto_ktp`,`foto_nasabah`,`status`) VALUES ('$id_dropping', '$name', '$alamat', '$tr', '$parkiran', '$resort', '$pinjaman','$latitude', '$longitude', '$file_url', '$file_url2', '$status');";


            //adding the path and name to database 
            //if(mysqli_query($con,$sql))
            if($stmt->execute()){

                //filling response array with values 
                $response['error'] = false; 
                $response['url'] = $file_url; 
                $response['url2'] = $file_url2;
                $response['name'] = $name;
            }
            else{
                $error = $mysqli->errno . ' ' . $mysqli->error;
echo $error; // 1054 Unknown column 'foo' in 'field list'
        //if some error occurred 
        }}catch(Exception $e){
            $response['error']=true;
            $response['message']=$e->getMessage();
        }       
            //displaying the response 
            echo json_encode($response);

            //closing the connection 
            mysqli_close($con);
        }else{
            $response['error']=true;
            $response['message']='Please choose a file';
        }
    }

i expect the output to be like this {"error":false,"url":"http:\/\/192.168.43.149\/opr57\/upload\/9.jpg","url2":"http:\/\/192.168.43.149\/opr57\/upload\/9.jpg""name":"Bayu"}, but the actual output is like this

    <br />
<b>Fatal error</b>:  Uncaught Error: Call to a member function bind_param() on bool in D:\xampp\htdocs\opr57\upload.php:63
Stack trace:
#0 {main}
  thrown in
<b>D:\xampp\htdocs\opr57\upload.php</b> on line
<b>63</b>
<br />

I'm really looking forward for an answer. Thank you very much

</div>
  • 写回答

2条回答 默认 最新

  • duanlu0559 2019-06-20 14:05
    关注

    I've changing my code to using prepared statement and it worked like a charm

    here's where i change my code

    Before

    try{
                //saving the file 
                move_uploaded_file($_FILES['image']['tmp_name'],$file_path);
                move_uploaded_file($_FILES['image2']['tmp_name'],$file_path2);
                //$stmt = $conn->prepare("INSERT INTO dropping(id_dropping, nama, alamat, tr, parkiran, resort , pinjaman ,latitude,longitude,foto_ktp,foto_nasabah status) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)");
                //$stmt->bind_param("ssssssssssss", $id_dropping,$name,$alamat,$tr,$parkiran,$resort,$pinjaman,$latitude,$longitude,$file_url,$file_url2,$status);
                $sql = "INSERT INTO `opr57`.`dropping` (`id_dropping`,`nama`,`alamat`,`tr`,`parkiran`,`resort`,`pinjaman`,`latitude`,`longitude`,`foto_ktp`,`foto_nasabah`,`status`) VALUES ('$id_dropping', '$name', '$alamat', '$tr', '$parkiran', '$resort', '$pinjaman','$latitude', '$longitude', '$file_url', '$file_url2', '$status');";
    
    
                //adding the path and name to database 
                //if($stmt->execute())
                if(mysqli_query($con,$sql)){
    
                    //filling response array with values 
                    $response['error'] = false; 
                    $response['url'] = $file_url; 
                    $response['url2'] = $file_url2;
                    $response['name'] = $name;
                }
    
    
            //if some error occurred 
            }catch(Exception $e){
                $response['error']=true;
                $response['message']=$e->getMessage();
            }
    

    After

            try{
                //saving the file 
                move_uploaded_file($_FILES['image']['tmp_name'],$file_path);
                move_uploaded_file($_FILES['image2']['tmp_name'],$file_path2);
                $stmt ="INSERT INTO dropping(id_dropping, nama, alamat, tr, parkiran, resort , pinjaman ,latitude,longitude,foto_ktp,foto_nasabah,status) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
                //$stmt->bind_param("ssssssssssss", $id_dropping,$name,$alamat,$tr,$parkiran,$resort,$pinjaman,$latitude,$longitude,$file_url,$file_url2,$status);
                //$sql = "INSERT INTO `opr57`.`dropping` (`id_dropping`,`nama`,`alamat`,`tr`,`parkiran`,`resort`,`pinjaman`,`latitude`,`longitude`,`foto_ktp`,`foto_nasabah`,`status`) VALUES ('$id_dropping', '$name', '$alamat', '$tr', '$parkiran', '$resort', '$pinjaman','$latitude', '$longitude', '$file_url', '$file_url2', '$status');";
    
    
                //adding the path and name to database 
                //if(mysqli_query($con,$sql))
                if($stat = $conn->prepare($stmt)){
                    $stat->bind_param("ssssssssssss", $id_dropping,$name,$alamat,$tr,$parkiran,$resort,$pinjaman,$latitude,$longitude,$file_url,$file_url2,$status);
                    $stat->execute();
                    //filling response array with values 
                    $response['error'] = false; 
                    $response['url'] = $file_url; 
                    $response['url2'] = $file_url2;
                    $response['name'] = $name;
                }
                else{
                    $error = $conn->errno . ' ' . $conn->error;
                    echo $error; 
            //if some error occurred 
            }}catch(Exception $e){
                $response['error']=true;
                $response['message']=$e->getMessage();
            }
    

    I've decide to use prepared statement based on @Dharman Advice thanks to that i am changing my code to above code

    well, actually my problem with that code before is just some typo on status where there is another comma after status and there's no comma before status

    I give many thanks to everyone who's giving me advice and answer to this problem

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。