使用Volley从Android应用程序将数据插入mysql数据库

时间:2018-08-05 17:51:07

标签: php android mysql android-volley

我正在尝试将android应用中的一行插入mysql数据库,并通过localhost在xampp服务器上运行。 我的数据库连接正确,并且php文件的路径也正确。 但是我无法将数据从应用程序上传到mysql。

这是我的代码:

InsertActivity.java:

public class InsertActivity extends AppCompatActivity {
EditText eid,ename,eprice,equantity,esupemail,esupphone;
Button addbtn;
ProgressDialog progressDialog;
String newname,newprice,newqnty,newem,newno;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_insert);
    eid=findViewById(R.id.ETid);
    ename=findViewById(R.id.ETname);
    eprice=findViewById(R.id.ETprice);
    equantity=findViewById(R.id.ETquantity);
    esupemail=findViewById(R.id.ETsupemail);
    esupphone=findViewById(R.id.ETsupphone);
    addbtn=findViewById(R.id.addnewbtn);

    progressDialog = new ProgressDialog(InsertActivity.this);

    addbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            progressDialog.setMessage("Please Wait, We are Inserting Your 
            Data on Server");
            progressDialog.show();
            GetValueFromEditText();

            // Creating string request with post method.
            StringRequest stringRequest = new 
            StringRequest(Request.Method.POST, Url.Insert_URL,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String ServerResponse) {
                            Log.i("tag",ServerResponse);

                            // Hiding the progress dialog after all task 
                               complete.
                            progressDialog.dismiss();

                            try {
                                JSONObject jsonObject = new 
                                JSONObject(ServerResponse);

                                Toast.makeText(getApplicationContext(), 
                                jsonObject.getString("message"), 
                                Toast.LENGTH_LONG).show();

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {

                            // Hiding the progress dialog after all task 
                               complete.
                            progressDialog.dismiss();

                            // Showing error message if something goes 
                               wrong.
                            Toast.makeText(InsertActivity.this, 
                            volleyError.toString(), 
                            Toast.LENGTH_LONG).show();
                        }
                    }) {
                @Override
                protected Map<String, String> getParams() {

                    // Creating Map String Params.
                    Map<String, String> params = new HashMap<String, String> 
                    (); 

                    // Adding All values to Params.
                    params.put("ename", newname);
                    params.put("eprice", newprice);
                    params.put("equantity", newqnty);
                    params.put("esupemail", newem);
                    params.put("esupphone", newno);

                    return params;
                }

            };

            // Creating RequestQueue.
            RequestQueue requestQueue = 
            Volley.newRequestQueue(InsertActivity.this);

            // Adding the StringRequest object into requestQueue.
            requestQueue.add(stringRequest);

        }

    });

}
// Creating method to get value from EditText.
public void GetValueFromEditText(){

    newname = ename.getText().toString().trim();
    newprice = eprice.getText().toString().trim();
    newqnty = equantity.getText().toString().trim();
    newem = esupemail.getText().toString().trim();
    newno = esupphone.getText().toString().trim();

}
}

这是我用来插入数据的php文件:

    <?php

        include ("dbconnect.php");

        $response = array(); 

        if($_SERVER['REQUEST_METHOD'] == 'POST'){

        $name = $_POST['ename'];
        $price = $_POST['eprice'];
        $qnty = $_POST['equantity'];
        $email = $_POST['esupemail'];
        $phone = $_POST['esupphone'];

        $adding = "INSERT INTO 
        information(ID,Name,Price,Quantity, 
        Supplier,Sup_Phone,Stockin,Stockout) VALUES(NULL,'$name','$price', 
       '$qnty','$email','$phone',NULL,NULL)";

       $result = mysqli_query($dbconnect,$adding);
        if($result>0)
        {
        $response['error'] = false;
        $response['message']="ADDED Successfully"
        }
        else{
        $response['error'] = true;
        $response['message']="Error in Inserting"
        }   
       }
       ?>

0 个答案:

没有答案