如何使用改造2更新mysql数据库的行?

时间:2019-06-09 14:01:10

标签: php android retrofit2

在本教程之后,我尝试使用update中的retrofit2 android studio create mysql数据库的特定行:https://www.youtube.com/watch?v=xKEFGsMUG8s。我使用readupdate方法做到了这一点,并且可以使用,但是对于API interface来说,我遇到了更多困难。我有一个updateTest和一个php脚本,可以将请求发送到我的数据库。

我的问题是:当我通过单击按钮使用方法public interface JsonPlaceHolderApiTest { @PUT("update.php/{id}") Call<Test> putTest(@Path("id") int id, @Body Test test); } 时,响应告诉我请求已成功,但是我的数据库中没有更新的行。

这是API接口:

private void updateTest(){

    Test test = new Test ("updated_name", "updated_age");

    Call<Test> call = jsonPlaceHolderApiTest.putTest(3, test);

    call.enqueue(new Callback<Test>() {
        @Override
        public void onResponse(Call<Test> call, Response<Test> response) {
            Toast.makeText(ActivityTest.this, response.toString(), Toast.LENGTH_LONG).show();

        }

        @Override
        public void onFailure(Call<Test> call, Throwable t) {
            Toast.makeText(ActivityTest.this, t.toString(), Toast.LENGTH_LONG).show();
        }
    });
}

这是更新方法:

<?php

mysqli_set_charset($db, 'utf8');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $id = $_POST['id'];
    $name = $_POST['name'];
    $age = $_POST['age'];

    require_once 'db_config.php';

    $sql = "UPDATE `test` SET name= '$name', age = '$age' WHERE id = '$id'";
    $check = mysqli_fetch_array(mysqli_query($db, $sql));
    if (mysqli_query($check)) {
        $response["success"] = true;
        $response["message"] = "successfully";
    } else {
        $response["success"] = false;
        $response["message"] = "Failure!";
    }
} else {
    $response["value"] = false;
    $response["message"] = "Error!";
}

echo json_encode($response);

这是php脚本:

{{1}}

1 个答案:

答案 0 :(得分:0)

我通过更改php脚本,API接口和更新方法找到了解决方案。

<?php

include("db_config.php");


function updateData($id,$name,$age){
global $db;
$result=mysqli_query($db , "UPDATE `test` SET `name`='$name',
`age`='$age' WHERE id = '$id'")or die(mysqli_error($db));
return $result;
}

$id = $_REQUEST["id"];
$name = $_REQUEST['name'];
$age = $_REQUEST['age'];

if (isset($id) && isset($name) && isset($age)){
    $result=updateData($id,$name,$age);

if ($result) {
    $response["success"] = 1;
    $response["message"] = "Successfully updated"; 
    echo json_encode($response);
} 
else {
    $response["success"] = 0;
    $response["message"] = "Oops! An error occurred."; 
    echo json_encode($response);
}

} 
else {
  $response["success"] = 0;
  $response["message"] = "Required field(s) is missing";  
  echo json_encode($response);
}
?>

在API界面中,我已将@PUT替换为@POST

@FormUrlEncoded
@POST("update.php")
Call<Test> updateTest2(
        @Field("id") int id,
        @Field("name")String name,
        @Field("age")String age);

这是新的更新方法:

 private void updateTest2(int id, String name, String age) {


    JsonPlaceHolderApiTest jsonPlaceHolderApiTest =ApiClient.getApiClient().create(JsonPlaceHolderApiTest.class);
    Call<Test> call = jsonPlaceHolderApiTest.updateTest2(id, name, age);

    call.enqueue(new Callback<Test>() {
        @Override
        public void onResponse(Call<Test> call, Response<Test> response) {

            Toast.makeText(ActivityTest.this, response.toString(), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onFailure(Call<Test> call, Throwable t) {
            Toast.makeText(ActivityTest.this, t.toString(), Toast.LENGTH_LONG).show();
        }
    });

}