PHP未收到翻新过的数据

时间:2019-03-15 20:23:14

标签: php android retrofit

我想通过翻新来更新表,所以我有如下所示的API

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

我的回叫实现在哪里

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

我的PHP文件是

<?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';
}

,但是在PHP中它根本不接收数据。在邮递员中,我还测试了它是否为所有数据字段都指定了未定义的索引stdid。请帮帮我。预先感谢一百万

1 个答案:

答案 0 :(得分:0)

从您的请求Callback<Response> callback);中删除regStudent()

因此您的API请求类将位于

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

您必须排队请求,然后才将其发送到服务器。


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

我希望这会有所帮助