【发布时间】:2016-07-30 07:02:47
【问题描述】:
我正在尝试修改我的数据库 (MySQL) 的值。对于这个问题,我在我的Android 应用程序上使用HttPut 和Slim framework 作为PHP 的微框架,XAMPP 用于管理数据库。
问题在于它没有修改我的数据库,但是当我使用 response.getStatusLine().getStatusCode() 时它给了我状态 200。
如果我不理解,当您收到状态 200 时,那是因为请求已成功。正如你在这里看到的:status 200 OK
请求成功。
我错过了什么吗?我理解错了吗?为什么数据库没有变化?
P.S.如果您需要我提供一些代码,请在评论中告诉我。
编辑:这是我在PHP script 中的put 方法。
$app->put("/cars/",function() use($app)
{
$name = $app->request->put("name");
$description = $app->request->put("description");
$idCar = $app->request->put("idCar");
try{
$connection = getConnection();
$dbh = $connection->prepare("UPDATE cars SET name = ?, description = ? WHERE idCar = ?");
$dbh->bindParam(1,$name);
$dbh->bindParam(2,$description);
$dbh->bindParam(3,$idCar);
$dbh->execute();
$connection = null;
header("HTTP/1.1 200");
header("Content-Type:application/json; charset=utf-8");
echo json_encode(array('status' => true),JSON_UNESCAPED_UNICODE );
}catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
});
编辑 2:这是我使用 HttpPut 的代码。
class EditCar extends AsyncTask<Void, Integer, Void> {
private Car editCar;
EditCar(Car editCar) {
this.editCar = editCar;
}
protected void onPreExecute() {
}
protected Void doInBackground(Void... params) {
try {
String url = "http://IP of my computer/project/cars/";
HttpClient httpClient = new DefaultHttpClient();
HttpPut method = new HttpPut(url);
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(new BasicNameValuePair("name", editCar.getName()));
nameValuePairList.add(new BasicNameValuePair("description", editCar.getDescription()));
nameValuePairList.add(new BasicNameValuePair("idCar", Integer.toString(editCar.getIdCar())));
method.setEntity(new UrlEncodedFormEntity(nameValuePairList));
HttpResponse response = httpClient.execute(method);
} catch (Exception ex) {
Log.e("Error", ex.toString());
}
return null;
}
protected void onProgressUpdate() {
}
protected void onPostExecute() {
}
}
【问题讨论】:
-
你能显示php代码吗?此外,根据您设置的标题(如果有),如果它找到并运行 php 文件,它会返回 200 OK。另外,尝试将 PHP 错误报告放在脚本的顶部 -
ini_set('display_errors', 1); error_reporting(-1);。 -
@Darren 当然,我现在将使用
put方法编辑我的问题。我必须把你的ini_set和error_reporting放在哪里?在我所有的方法之前? -
是的,在您的
<?php开始标签之后的第一件事。 -
UPDATE不会返回$connection->lastInsertId(),因为没有插入任何内容,这可能会导致问题(返回的 json 将为空。)。您究竟需要什么才能返回您的应用程序? -
@Darren 我以前没有,但如果没有,我不知道我要穿什么
echo json_encode(//here,JSON_UNESCAPED_UNICODE);