邮递员的邮递要求传回null

时间:2019-06-20 07:58:31

标签: php mysql post postman

我正在制作一个可以将多个图像上传到服务器的android应用程序,以确保我的php正常工作,我正在邮递员上尝试它,但是当我在邮递员上尝试它时响应为空

这是我从邮递员那里得到的答复

this is the body

this is the result i get

我已经阅读了一些该应用程序教程的网站,并编写了与php完全相同的php,但是在我的发帖请求中它始终发送null

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

这是我的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';
        }
    }

我希望输出像这样{"error":false,"url":"http:\/\/192.168.43.149\/opr57\/upload\/9.jpg","url2":"http:\/\/192.168.43.149\/opr57\/upload\/9.jpg""name":"Bayu"},但是实际输出像这样

    <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 />

我真的很期待一个答案。 非常感谢

2 个答案:

答案 0 :(得分:1)

我已将代码更改为使用预备语句,它的工作原理很吸引人

这是我更改代码的地方

之前

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

之后

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

由于我将代码更改为上述代码,因此我决定使用基于@Dharman Advice的准备好的语句

好吧,实际上我之前的代码问题只是status上的一些错字,在状态后还有另一个逗号,而status之前没有逗号

我非常感谢每个给我建议和回答这个问题的人

答案 1 :(得分:-1)

首先,删除或注释此代码

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

然后将其添加到您的代码中。

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 );