Php 已成功更新响应,但未更新表
我正在用android开发应用程序。从那里,我想更新表中的电子邮件 数据库在MySQL中 我通过在浏览器中执行来检查我的PHP代码。 它可以正常工作并更新我的数据库,但当 我使用android运行文件,但它不更新表 但返回响应成功更新 我的Android代码Php 已成功更新响应,但未更新表,php,android,mysql,json,Php,Android,Mysql,Json,我正在用android开发应用程序。从那里,我想更新表中的电子邮件 数据库在MySQL中 我通过在浏览器中执行来检查我的PHP代码。 它可以正常工作并更新我的数据库,但当 我使用android运行文件,但它不更新表 但返回响应成功更新 我的Android代码 /** * Background Async Task to Create new product * */ class UpdateEmail extends AsyncTask<String, String, String&g
/**
* Background Async Task to Create new product
* */
class UpdateEmail extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(edit_profile.this);
pDialog.setMessage("Updating....");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
String abc= email_edit_view.getText().toString();
SessionSharedPrefences.setUserEmail(getApplicationContext(), abc);
Log.i("email_edit_view", abc);
params.add(new BasicNameValuePair("Email", "abc@gmail.com"));
params.add(new BasicNameValuePair("U_ID", SessionSharedPrefences.getUserEmail(getApplicationContext())));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(update_url_email_information ,"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try
{
int success = json.getInt(TAG_SUCCESS);
if (success == 1)
{
Log.i("sucessssssssss", "sucess");
// closing this screen
}
else
{
// failed to create product
}
}
catch (JSONException e)
{
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
if (pDialog.isShowing()) {
pDialog.dismiss();
// progressDialog.setCancelable(true);
}
}
}
/**
*创建新产品的后台异步任务
* */
类UpdateEmail扩展了异步任务{
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(编辑配置文件。此对话框);
pDialog.setMessage(“更新…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
/**
*创造产品
* */
受保护的字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
字符串abc=email\u edit\u view.getText().toString();
SessionSharedPrefences.setUserEmail(getApplicationContext(),abc);
Log.i(“电子邮件编辑视图”,abc);
参数添加(新的BasicNameValuePair(“电子邮件”)abc@gmail.com"));
add(新的BasicNameValuePair(“U_ID”,SessionSharedPrefences.getUserEmail(getApplicationContext())));
//获取JSON对象
//请注意,创建产品url接受POST方法
JSONObject json=jsonParser.makeHttpRequest(更新url电子邮件信息,“POST”,参数);
//检查cat fro响应日志
d(“创建响应”,json.toString());
//检查成功标签
尝试
{
int success=json.getInt(TAG_success);
如果(成功==1)
{
Log.i(“successss”、“success”);
//关闭此屏幕
}
其他的
{
//未能创建产品
}
}
捕获(JSONException e)
{
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//完成后关闭对话框
if(pDialog.isShowing()){
pDialog.disclose();
//progressDialog.setCancelable(真);
}
}
}
我的PHP文件是
<?php
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['U_ID']) && !empty($_POST['U_ID'])&& isset($_POST['Email']) && !empty($_POST['Email']) ) {
$U_ID = $_POST['U_ID'];
$Email_id = $_POST['Email'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
//$result = mysql_query("UPDATE `user` SET `Email_id`='$Email_id' WHERE `U_id`= '$U_ID'");
$result = mysql_query("UPDATE `smartshoppingsystem`.`user` SET `Email_id` = '$Email_id' WHERE `user`.`U_id` ='$U_ID'");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Data successfully Updates.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
如果你真的得到了一个成功的响应,我猜你的android应用程序正在传输一个数据库中不存在的U_ID。这是我在params.add(new BasicNameValuePair(“U_ID”,SessionSharedPrefences.getUserEmail(getApplicationContext()))行的错误;我正在使用EmailId作为用户ID
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
Log.d("JSONParser","POST METHOD ");
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.d("JSONParser is",is.toString());
}else if(method == "GET"){
Log.d("JSONParser","GET METHOD ");
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.d("JSONParser is",is.toString());
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}