我正在制作一个Android应用程序,我正在尝试使用PHP和JSON更新MySQL数据库中的user_group信息。我使用的方法是成功更新数据库,我从addHome()调用它的情况,但是当我在joinExistingHome()方法中调用相同的方法时,我无法添加到数据库... < / p>
以下是我的PHP代码的重要部分:
// include db handler
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
else if ($tag == "update") {
$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);
}
/**
*Updating a user's group
*/
public function updateUser($user_group, $email) {
$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;
}
}
这是我尝试更新数据库的java代码:
public void addHome() {
//make a new dialog popup and user can enter a new group name.
dialog = new Dialog(context);
dialog.setContentView(R.layout.set_home_dialog);
dialog.setTitle("Add a new home");
dialog.show();
cancelButton = (Button) dialog.findViewById(R.id.cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
submitButton = (Button) dialog.findViewById(R.id.submitGroup);
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);
if (groupNames.contains(name)) {
System.out.println("The group already exists please enter a new group name");
return;
}
System.out.println("Trying to update user group");
addGroupToDatabases(name);
}
});
//System.out.println("here2");
}
public void joinExistingHome() {
//new dialog appears and prompt user to enter existing group name
//make a new dialog popup and user can enter a new group name.
dialog = new Dialog(context);
dialog.setContentView(R.layout.set_home_dialog);
dialog.setTitle("Enter existing home");
dialog.show();
cancelButton = (Button) dialog.findViewById(R.id.cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
submitButton = (Button) dialog.findViewById(R.id.submitGroup);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//need to check if the groupname is in sqlite database.
EditText groupName = (EditText) dialog.findViewById(R.id.group);
final String name = groupName.getText().toString();
System.out.println("available groups: " + groupNames);
if (name != null && groupNames.contains(name) && !name.isEmpty()) {
addGroupToDatabases(name);
} else {
System.out.println("No group with group name: " + name + " exists");
}
}
});
}
public void addGroupToDatabases(final String name) {
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();
}
}) {
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);
System.out.println("User's email is: " + 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();
}