duanmen1887 2016-07-27 02:00 采纳率: 0%
浏览 59

更新wampserver中的值

I am now trying to make my points to update automatically when a new order is sent to database . I have created a php file and tried doing it but nothing happens. I am quite new and unsure on how to continue and what to add in the activity. Please help thanks .

database

php file :

 <?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;
}

?>

activity codes :

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);



}



}
  • 写回答

1条回答 默认 最新

  • dpl74687 2016-07-27 04:35
    关注

    Just use UPDATE instead of INSERT .

    Your sql statement should be like....

    $sql = "UPDATE ".$table_name." SET points='".$newpoints."' WHERE membership = '".$membership."'";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM