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

邮差请求返回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

    点赞 评论
  • douduikai0562 2019-06-20 08:48

    First, remove or comment this code

    //displaying the response 
    echo json_encode($response);

    Then add this to your code bro..

    header( 'Content-Type: application/json' );
    return print json_encode( $response );

    <?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(); 
    $response['error'] = true; 
    
    
    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;
    
    
            //saving the file 
            move_uploaded_file($_FILES['image']['tmp_nama'],$ktppath);
            move_uploaded_file($_FILES['image']['tmp_nama'],$nasabahpath);
            $sql = "INSERT INTO `opr57`.`dropping` (`id_dropping`,`nama`,`alamat`,`tr`,`parkiran`,`resort`,`pinjaman`,`latitude`,`longitude`,`foto_ktp`,`foto_nasabah`,`status`) 
                VALUES ('$id_dropping', '$nama', '$alamat', '$tr', '$parkiran', '$resort', '$pinjaman','$latitude', '$longitude', '$ktpurl', '$nasabahurl', '$status',);";
    
                //adding the path and name to database 
                if(mysqli_query($con,$sql)){
                    //filling response array with values 
                    $response['error'] = false; 
                    $response['url'] = $ktpurl;                
                    $response['name'] = $nama;
                }
    
            //closing the connection 
            mysqli_close($con);
        }else{
            $response['message']='Please choose a file';
        }
    }
    
    header( 'Content-Type: application/json' );
    return print json_encode( $response );

    </div>
    
    点赞 评论

相关推荐 更多相似问题