我正在编写一个Android应用程序,并且我能够使用以下PHP
的片段将特定用户的user_group更新到我的MySQL数据库:
else if ($tag == "update") {
error_reporting(0);
$user_group = $_POST['user_group'];
$email = $_POST['email'];
//update user
$user = $db->updateUser($user_group, $email);
if ($user) {
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["user_group"] = ["user"]["user_group"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Error occured in Updating user group";
echo json_encode($response);
}
public function updateUser($user_group, $email) {
error_reporting(0);
$result = mysql_query("UPDATE users SET user_group = '$user_group' WHERE email = '$email'");
if ($result) {
// get user details
//$uid = mysql_insert_id(); // last inserted id
$result = mysql_query("SELECT * FROM users WHERE email = '$email'");
// return user details
return mysql_fetch_array($result);
} else {
return false;
}
}
但后来我得到了JSON响应,它说user_group为null:
Update Response: {"tag":"update","error":false,"uid":"55c9a7be642501.18311962","user":{"name":"Hi","email":"hi","user_group":null,"created_at":"2015-08-11 00:43:58","updated_at":null}}
这是我获取响应的java代码:
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText groupName = (EditText) dialog.findViewById(R.id.group);
final String name = groupName.getText().toString();
System.out.println("Name of group is: " + name);
updateUser(name);
System.out.println("Trying to update user group");
if (!name.isEmpty()) {
//add to sqlite database
updateUser(name);
} else {
Toast.makeText(getApplicationContext(),
"Please enter your details!", Toast.LENGTH_LONG)
.show();
}
String tag_string_req = "req_update";
//showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Update Response: " + response.toString());
//hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
System.out.println("User group successfully updated in mysql");
// User successfully stored in MySQL
} else {
System.out.println("The use group failed to update");
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Update Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
// hideDialog();ooo
}
}) {
String email = db.getUserDetails().get("email");
@Override
protected Map<String, String> getParams() {
System.out.println("trying to return params");
// Posting params to register url
Map<String, String> params = new HashMap<>();
params.put("tag", "update");
params.put("user_group", name);
params.put("email", email);
return params;
}
};
System.out.println("updating group to mysql database");
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
// System.out.println("here1");
// System.out.println("user group: " + db.getUserDetails().get("user_group"));
dialog.dismiss();
}
});
//System.out.println("here2");
}
答案 0 :(得分:3)
$response["user"]["user_group"] = ["user"]["user_group"];
替换为:
$response["user"]["user_group"] = $user["user_group"];