Lab 6 Codes Part 2 (AddProduct)
create_productVolley.php
<?php if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description']) ) { $name = $_POST['name']; $price = $_POST['price']; $descp = $_POST['description']; require_once __DIR__ . '/db_connect.php'; // include db connect class $db= new DB_CONNECT();// connecting to db $db->connect(); // get the search product from products table $sqlCommand = "INSERT INTO products (name, price, description) VALUES ('$name', '$price', '$descp')"; $result =mysqli_query($db->myconn, "$sqlCommand"); if ($result) { //if result is successful echo ("Success"); } else echo ("Error"); $db->close($db->myconn); } else { echo ("Error"); } ?>
NewProductActivity.java
package mdad.localdata.volleysqldatabase1; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.activity.EdgeToEdge; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import java.util.HashMap; import java.util.Map; public class NewProductActivity extends AppCompatActivity { EditText inputName; EditText inputPrice; EditText inputDesc; Button btnCreateProduct; String name ,price,description; // url_create_product = "http://172.30.103.225/products/create_productVolley.php"; private static String url_create_product =MainActivity.ipBaseAddress+"/create_productVolley.php"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_new_product); // retrieve the resource id for EditText inputName = (EditText) findViewById(R.id.inputName); inputPrice = (EditText) findViewById(R.id.inputPrice); inputDesc = (EditText) findViewById(R.id.inputDesc); // retrieve the resource id for button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct); // button click event btnCreateProduct.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //retrieve the values in EditText entered by user name = inputName.getText().toString(); price = inputPrice.getText().toString(); description = inputDesc.getText().toString(); //check for no empty values in EditText if (name.isEmpty() || price.isEmpty() || description.isEmpty()) { Toast.makeText(getApplicationContext(), "11y", Toast.LENGTH_LONG).show(); return; } //put the product info as key-values pair in HashMap Map<String,String> params_create = new HashMap<String,String>(); params_create.put("name",name); params_create.put("price",price); params_create.put("description",description); //postData method to use Volley to update new product details in database postData(url_create_product,params_create); } }); }//end of onCreate() public void postData(String url, Map params){ //create a RequestQueue for Volley RequestQueue requestQueue = Volley.newRequestQueue(this); //create StringRequest for http post web request to send new product info to database StringRequest stringRequest = new StringRequest(Request.Method.POST, url, //response from server new Response.Listener<String>() { @Override public void onResponse(String response) { if (response.equals("Error")) { Toast.makeText(getApplicationContext(),"Error in updating database", Toast.LENGTH_LONG).show(); } if(response.equals("Success")) { Toast.makeText(getApplicationContext(),"Success in updating database", Toast.LENGTH_LONG).show(); finish(); //load the AllProductActivity with updated ListView Intent i = new Intent (getApplicationContext(), AllProductsActivity.class); startActivity(i); } } }, //error in Volley new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // handle error Toast.makeText(getApplicationContext(),"Error in accessing database",Toast.LENGTH_LONG).show(); } } ) { @Nullable @Override // to send product info stored in HashMap params_create to server via HTTP Post protected Map<String, String> getParams() { return params; } }; //add StringRequest to Volley Queue requestQueue.add(stringRequest); } }// end of class
Comments
Post a Comment