PHP代码将重复行插入MySQL

时间:2014-02-23 16:40:38

标签: php android mysql sql

我有一个PHP代码,我的android应用程序调用它来将食物订单插入到MySql数据库中。但是,代码插入两行用于相同的顺序(复制)。这是我的代码......

 <?php
$hostname_localhost ="localhost";
$database_localhost ="on2campusdb";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_localhost, $localhost); 
$userID = $_POST['userID'];
$item1=$_POST['item1'];
$item2=$_POST['item2'];
$item3=$_POST['item3'];
$item4=$_POST['item4'];
$item5=$_POST['item5'];
$costTotal=$_POST['costTotal'];
$block=$_POST['block'];
$query_insert = "INSERT into canteen_orders (PlacedBy, Item1, Item2, Item3, Item4,   Item5, Total,Block) VALUES     ('$userID','$item1','$item2','$item3','$item4','$item5','$costTotal','$block')";

if(mysql_query($query_insert)) { 
echo "Order Placed"; 
}
else  {
echo "Order Failed"; 
}
?>

我无法弄清楚原因。任何帮助将不胜感激:)谢谢!

@Override
protected void onCreate(Bundle savedInstanceState) {
    final String n1 = DatabaseHandler.name1;
    final String n2 = DatabaseHandler.name2;
    final String n3 = DatabaseHandler.name3;
    final String n4 = DatabaseHandler.name4;
    final String n5 = DatabaseHandler.name5;
    String[] NAMES = { n1, n2, n3, n4, n5 };
    Integer TOTAL;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_confirm);
    btnCancelOrder = (Button) findViewById(R.id.btnCancel);
    btnPlaceOrder = (Button) findViewById(R.id.btnPlace);
    costTotal = (TextView) findViewById(R.id.orderTotal);
    try {

        for (String hey : NAMES) {
            Log.i("hey", "" + hey);
        }
        try {
            lstView = (ListView) findViewById(R.id.listSummary);
            TOTAL = DatabaseHandler.costTotal;
            Log.i("hey", "got TOTAL");
            try {
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        Confirm.this, android.R.layout.simple_list_item_1,
                        NAMES);

                lstView.setAdapter(adapter);

                costTotal.setText(TOTAL.toString(), null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    btnCancelOrder.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            DatabaseHandler.resetStrings(n1,n2,n3,n4,n5);
            DatabaseHandler.resetCost();
            DatabaseHandler.resetCount();
            Intent intent = new Intent(Confirm.this, Welcome.class);
            startActivity(intent);
        }

    });
    btnPlaceOrder.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog = ProgressDialog.show(Confirm.this, "",
                    "Placing Order...", true);
            new Thread(new Runnable() {
                public void run() {
                    placeOrder();
                }

            }).start();
        }
    });
}

private void placeOrder() {

    try {

        httpclient = new DefaultHttpClient();
        httppost = new HttpPost("http://10.0.2.2/on2campus/placeOrder.php"); // add
                                                                                // data
                                                                                // nameValuePairs
                                                                                // =
        items = new ArrayList<NameValuePair>(8);

        items.add(new BasicNameValuePair("userID", userID)); // $Edittext_value
                                                                // = //
                                                                // $_POST['Edittext_value'];
        items.add(new BasicNameValuePair("item1", n1.toString().trim()));
        items.add(new BasicNameValuePair("item2", n2.toString().trim()));
        items.add(new BasicNameValuePair("item3", n3.toString().trim()));
        items.add(new BasicNameValuePair("item4", n4.toString().trim()));
        items.add(new BasicNameValuePair("item5", n5.toString().trim()));
        items.add(new BasicNameValuePair("costTotal", costTotal.getText()
                .toString().trim()));
        items.add(new BasicNameValuePair("block", "testblock"));
        httppost.setEntity(new UrlEncodedFormEntity(items)); // Execute HTTP
                                                                // Post
                                                                // Request
        response = httpclient.execute(httppost);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost,responseHandler);
        System.out.println("Response : " + response);

        runOnUiThread(new Runnable() {
            public void run() {

                dialog.dismiss();
            }
        });

        if (response.equalsIgnoreCase("Order Placed")) {
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(Confirm.this, "Order Placed",
                            Toast.LENGTH_SHORT).show();
                }
            });
            Intent intent = new Intent(Confirm.this, Welcome.class);
            startActivity(intent);
        } else {
            showAlert();
        }

    } catch (Exception e) {
        dialog.dismiss();
        System.out.println("Exception : " + e.getMessage());
    }

}

public void showAlert() {
    Confirm.this.runOnUiThread(new Runnable() {
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(
                    Confirm.this);
            builder.setTitle("Order Error");
            builder.setMessage("Order Not Placed")
                    .setCancelable(false)
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                }
                            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.confirm, menu);
    return true;
}


 }

要回答vlzvl,我没有做任何事情来保护我的桌子免受多个条目的影响...... 此外,我还没有更改模拟器中的屏幕方向...它以纵向模式运行

0 个答案:

没有答案