download1214 2019-03-15 20:23
浏览 132
已采纳

PHP不接收改造后期数据

i want to update a table by using retrofit, so i have an API as follows

public interface StRegAPI {
@FormUrlEncoded
@POST("/stdreg.php")
public void regStudent(
        @Field("stdid") String stdid,
        @Field("stdpass") String stdpass,
        @Field("stdadd") String stdadd,
        @Field("stdphn") String stdphn,
        @Field("stdemail") String stdemail,
        Callback<Response> callback);
      }

where my call back implementation is

StRegAPI api = adapter.create(StRegAPI.class);

//Defining the method insertuser of our interface
api.regStudent(

        //Passing the values by getting it from editTexts
        rn_list.getSelectedItem().toString(),
        etstpass.getText().toString(),
        etstad.getText().toString(),
        etstphn.getText().toString(),
        etstemail.getText().toString(),


        //Creating an anonymous callback
        new Callback<Response>() {
            @Override
            public void success(retrofit.client.Response result, retrofit.client.Response response) {
                //On success we will read the server's output using bufferedreader
                //Creating a bufferedreader object
                BufferedReader reader = null;

                //An string to store output from the server
                String output = "";

                try {
                    //Initializing buffered reader
                    reader = new BufferedReader(new InputStreamReader(result.getBody().in()));

                    //Reading the output in the string
                    output = reader.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                //Displaying the output as a toast
                Toast.makeText(StudReg.this, output, Toast.LENGTH_LONG).show();
            }

            @Override
            public void failure(RetrofitError error) {
                //If any error occured displaying the error as toast
                Toast.makeText(StudReg.this, error.toString(),Toast.LENGTH_LONG).show();
            }
        }
);
  }

and my PHP file is

<?php
//checking if the script received a post request or not 
if($_SERVER['REQUEST_METHOD']=='POST'){

 //Getting post data 
 $stdid=$_POST['stdid'];
 $stdpass = $_POST['stdpass'];
 $stdadd = $_POST['stdadd'];
 $stdphn = $_POST['stdphn'];
 $stdemail=$_POST['stdemail'];

 //checking if the received values are blank
 if($stdid == '' || $stdpass== '' || $stdad == '' || $stdemail=='' || $stphn==''){
 //giving a message to fill all values if the values are blank
 echo 'please fill all values';
 }else{
 //If the values are not blank
 //Connecting to our database by calling dbConnect script 
 require_once('connection.php');

 //Creating an SQL Query to insert into database 
 //Here you may need to change the retrofit_users because it is the table I created
 //if you have a different table write your table's name

 //This query is to check whether the username or email is already registered or not 
 $sql = "SELECT * FROM student WHERE stud_id=$stdid";

 //If variable check has some value from mysqli fetch array 
 //That means username or email already exist 
 $check = mysqli_fetch_array(mysqli_query($con,$sql));

 //Checking check has some values or not 
 if(!(isset($check))){
 //If check has some value that means username already exist 
 echo 'studentid does not exist';
 }else{ 
 //If username is not already exist 
 //Creating insert query 
 $sql = "UPDATE student set password='$stdpass', addrs='$stdad',phn_no=$stdphn,email='$stdemail' WHERE stud_id=$stdid";

 //Trying to ins db 
 if(mysqli_query($con,$sql)){
 //If inserted successfully 
 echo 'successfully registered';
 }else{
 //In case any error occured 
 echo 'oops! Please try again!';
 }
 }
 //Closing the database connection 
 mysqli_close($con);
 }
}else{
echo 'error';
}

but in PHP it is not receiving the data at all. in postman also i tested it is specifying that undefined index stdid similarly for all the data fields. Please help me out. thanks a million in advance

  • 写回答

1条回答 默认 最新

  • dqqyp90576 2019-03-16 11:38
    关注

    Remove Callback<Response> callback); from your request regStudent().

    So your API request class would be below

    public interface StRegAPI {
    @FormUrlEncoded
    @POST("/stdreg.php")
    public void regStudent(
            @Field("stdid") String stdid,
            @Field("stdpass") String stdpass,
            @Field("stdadd") String stdadd,
            @Field("stdphn") String stdphn,
            @Field("stdemail") String stdemail);  
     }
    
    

    You have to enqueue your request then only it's will send data to server.

    
            Call<Response> call =api.regStudent(
    
            //Passing the values by getting it from editTexts
            rn_list.getSelectedItem().toString(),
            etstpass.getText().toString(),
            etstad.getText().toString(),
            etstphn.getText().toString(),
            etstemail.getText().toString());
    
            call.enqueue(new Callback<Response>() {
            @Override
            public void onResponse(Response<Response> response) {
                if (!response.isSuccess()) {
                    Log.d(LOG_TAG, "No Success");
                }
    
            }
    
            @Override
            public void onFailure(Throwable t) {
                // api failed
                  }
            });
    
    

    I hope it's would be helpful

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line