我正在尝试将新订单发送到数据库时自动更新。我已经创建了一个php文件并试着这样做但没有任何反应。我很新,不确定如何继续以及在活动中添加什么。 请帮助谢谢。
php文件:
<?php
include_once("connectorder.php");
if($_GET["membership"] )
{$membership = $_GET['membership'];
}
if($_GET["points"] )
{ $points = $_GET['points'];
}
$query=mysqli_query($con,"SELECT MAX(points) FROM ".$table_name." WHERE membership = '".$membership."'"); //Get current points
while($current=mysqli_fetch_assoc($query)){
$newpoints = $current['points'] + $points; //calculate the new points
}
$sql = "INSERT ".$table_name." SET points='".$newpoints."' WHERE membership = '".$membership."'"; //update the new points in database
if ($con->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
?>
活动代码:
public class MenuActivity extends Activity {
private String server = "http://172.16.156.56";
private String sql_table = "orders";
private ListView list;
private String txtOrder, txtMember, txtPrice;
private Button btnAdd;
ArrayList<String> rows;
ListView listView1;
Button btnSubmit;
ArrayList<CustomItem> itemList, selectedList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
list=(ListView) findViewById(R.id.listView);
btnAdd = (Button)findViewById(R.id.button);
rows = new ArrayList<String>();
itemList=new ArrayList<CustomItem>();
itemList.add(new CustomItem("Fried Rice","description","1","Quantity"));
itemList.add(new CustomItem("Fried Noodle","description","2","Quantity"));
itemList.add(new CustomItem("Prawn noodle","description","3","Quantity"));
itemList.add(new CustomItem("Chicken Rice","description","4","Quantity"));
int[] prgmImages={R.drawable.friedrice,R.drawable.friednoodle,R.drawable.pnoodle,R.drawable.chickenrice};
listView1 = (ListView) findViewById(R.id.listView);
final CustomLVAdapter customLVAdapter = new CustomLVAdapter(this, itemList,prgmImages);
listView1.setAdapter(customLVAdapter);
Bundle extras = getIntent().getExtras();
final String strMem = extras.getString("Mem");
btnSubmit = (Button) findViewById(R.id.button);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//go to ReceiptActivity - membership, item, totalprice
//Toast.makeText(getApplicationContext(), responseText + " $" + total,
// Toast.LENGTH_SHORT).show();
//SelectAll();
confirmdialog();
}
});
}
private void confirmdialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MenuActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Confirmation");
// Setting Dialog Message
alertDialog.setMessage("Submit order?");
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
//whatever you want to do
StringBuffer responseText = new StringBuffer();
selectedList = new ArrayList<CustomItem>();
int total = 0;
int points = 0;
for (int i = 0; i < itemList.size(); i++) {
CustomItem object = itemList.get(i);
if (object.isSelected()) {
responseText.append(object.getItem() + "," + object.getQty() + ",");//item
selectedList.add(object);
//calculate price
total = (total + Integer.parseInt(object.getQty()) * Integer.parseInt(object.getPrice()) );
points = (total + Integer.parseInt(object.getQty()) * Integer.parseInt(object.getPrice()) );
}
}
txtOrder = responseText.toString();
txtPrice = String.valueOf(total);
txtPrice = String.valueOf(points);
Bundle extras = getIntent().getExtras();
final String strMem = extras.getString("Mem");
Add(responseText.toString(), strMem, String.valueOf(total),String.valueOf(total) );
Intent i = new Intent(getApplicationContext(), ReceiptActivity.class);
i.putExtra("Order",txtOrder);
i.putExtra("Price",txtPrice);
i.putExtra("Mem",strMem);
startActivity(i);
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//cancel the dialog
dialog.dismiss();
}
});
alertDialog.show();
}
//store in database
public void Add(final String item, final String membership, final String price, final String points)
{
RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
String url = server + "/insertorder.php"+ "/update.php";
StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
protected Map<String, String> getParams() {
Map<String, String> MyData = new HashMap<String, String>();
MyData.put("item",item );
MyData.put("membership",membership );
MyData.put("price",price );
MyData.put("points",points);
return MyData;
}
};
MyRequestQueue.add(MyStringRequest);
SelectAll();
}
public void SelectAll()
{
RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
String url = server + "/fetchorder.php" ;
StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
JSONArray jsonMainNode = jsonResponse.optJSONArray(sql_table);
rows.clear();
for(int i=0; i < jsonMainNode.length(); i++)
{
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String order = jsonChildNode.optString("item").toString();
String membership = jsonChildNode.optString("membership").toString();
String price = jsonChildNode.optString("price").toString();
String points = jsonChildNode.optString("points").toString();
rows.add(order + ", " + membership + ", " + price + ", " + points);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MenuActivity.this,
android.R.layout.simple_list_item_1, rows);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
@Override
public void onErrorResponse(VolleyError error) {
//This code is executed if there is an error.
}
});
MyRequestQueue.add(MyStringRequest);
}
}
答案 0 :(得分:0)
只需使用UPDATE
代替INSERT
。
你的sql语句应该像....
$sql = "UPDATE ".$table_name." SET points='".$newpoints."' WHERE membership = '".$membership."'";