gpt4 book ai didi

php - 使用json在android中通过php更新mysql

转载 作者:行者123 更新时间:2023-11-30 22:42:50 24 4
gpt4 key购买 nike

我试图做一个改变mysql数据库的 Activity

我用这段代码做了一个网络服务器

<?php

//error_reporting(0);

require_once('db_config.php');


if($conn){


$sql = "UPDATE order_detail SET order_status ='completed', WHERE order_id = $ORDER_ID";

if(mysqli_query($conn,$sql)){

$response['Result'] =true;
$response['Message'] = 'Update Order Successfully';

}else{

$response['Result'] =false;
$response['Message'] = 'Unable to take order';

}

}else{

$response['Result'] =false;
$response['Message'] = 'Some Fields are missing';

}

}else{

$response['Result'] =false;
$response['Message'] = 'Unable to connect database';

}

echo json_encode($response);
mysqli_close($conn);


?>

这与 android Activity 有关以请求表扬,但我不知道为什么它不起作用

私有(private)无效 completeOrder() { //TODO 自动生成方法 stub

                if (Utils.checkConnection(this))
new OrderTask().execute();
else
Toast.makeText(this, "No Connection Found",
Toast.LENGTH_SHORT).show();

}


public class OrderTask extends AsyncTask<Void, Void, Boolean> {

private ProgressDialog pd;
private Boolean result = false;
private StringBuilder item_ids,item_names;

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();

pd = new ProgressDialog(SingleContactActivity.this);
pd.setMessage("Processing order,Please wait");
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setCancelable(false);
pd.show();
}

@Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub


HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(Utils.WS_UPDATE_ORDER);

List<NameValuePair> mParams = new ArrayList<NameValuePair>();
mParams.add(new BasicNameValuePair("order_status","Completed"));
mParams.add(new BasicNameValuePair("order_id",ORDER_ID.toString()));


try {

post.setEntity(new UrlEncodedFormEntity(mParams));

HttpResponse response = client.execute(post);

String res = EntityUtils.toString(response.getEntity());

Log.d("Order Preview", res);

JSONObject jsonObj = new JSONObject(res);

result = jsonObj.getBoolean("Result");



} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return result;
}

@Override
protected void onPostExecute(Boolean res) {
// TODO Auto-generated method stub
super.onPostExecute(res);

if (pd != null)
pd.dismiss();

if (res) {

Toast.makeText(SingleContactActivity.this,
"Order Submitted Successfully", Toast.LENGTH_SHORT)
.show();

Intent intent = new Intent(SingleContactActivity.this,
CategoryList.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

} else {

Toast.makeText(SingleContactActivity.this,
"Unable to process the order", Toast.LENGTH_SHORT)
.show();
}
}

}

最佳答案

您可能需要在查询结果中使用 mysqli_affected_rows() 因为 mysqli_query($conn,$sql) 总是返回您的查询结果并且您总是进入 如果条件。你必须计算受影响的行数。

还发现 最后一个 else 条件中的语法错误

可能是

<?php

//error_reporting(0);

require_once('db_config.php');
if ($conn) {


$sql = "UPDATE `order_detail` SET `order_status` ='completed', WHERE `order_id` = $ORDER_ID";

$result = mysqli_query($conn, $sql);
if (mysqli_affected_rows($result) > 0) {


$response['Result'] = true;
$response['Message'] = 'Update Order Successfully';
} else {

$response['Result'] = false;
$response['Message'] = 'Unable to take order';
}
} else {

$response['Result'] = false;
$response['Message'] = 'Some Fields are missing';
}

关于php - 使用json在android中通过php更新mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30636175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com