使用volley将数据从android应用程序插入mysql数据库

vuktfyat  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(187)

我正在尝试将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"
        }   
       }
       ?>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题