Insert data into database using Android,php & Mysql
In this tutorial i will explain a simple way to insert data into mysql database using Android,Php & Mysql.in this tutorial i use Volley library.
Copy paste the below code in your XML file.Here i am using Linear layout,you can use any layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
/>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Fathers Name"
android:inputType="textPersonName"
/>
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="MOthers Name"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Address"
android:gravity="start|top"
android:inputType="textMultiLine" />
<EditText
android:id="@+id/editText5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save" />
</LinearLayout>
Now code your mainactivity.java,the code is as follows:-
package com.example.demoinsert; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.android.volley.AuthFailureError; 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 MainActivity extends AppCompatActivity implements View.OnClickListener{ EditText name,fname,mname,address,email; Button b1; String url="http://192.168.43.229/youtube/insert.php"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name=(EditText)findViewById(R.id.editText); fname=(EditText)findViewById(R.id.editText2); mname=(EditText)findViewById(R.id.editText3); address=(EditText)findViewById(R.id.editText4); email=(EditText)findViewById(R.id.editText5); b1=(Button)findViewById(R.id.button); b1.setOnClickListener(this); } @Override public void onClick(View v) { final String sname=name.getText().toString(); final String fname1=fname.getText().toString(); final String mname1=mname.getText().toString(); final String address1=address.getText().toString(); final String email1=email.getText().toString(); RequestQueue queue = Volley.newRequestQueue(MainActivity.this); StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { Toast.makeText(MainActivity.this, "Insertion Is:" + response, Toast.LENGTH_SHORT).show(); Log.d("response",response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, "my error :" + error, Toast.LENGTH_LONG).show(); Log.i("My error", "" + error); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<String, String>(); map.put("name",sname); map.put("fname",fname1); map.put("mname",mname1); map.put("address",address1); map.put("email",email1); return map; } }; queue.add(request); } }Please remember that Your URL will be different.First find out the IP address of your PC,then usethat in the URL.You need to also receive the values which you are sending from the java file to the URL,Here i am
using POST method.Copy the below code for your php page.Here the name of the page is insert.php(which
is also mentioned in the URL)
<?php $host='localhost'; $uname='root'; $pwd=''; $db="youtube"; $con = mysqli_connect($host,$uname,$pwd,$db) or die("connection failed"); $stuname=$_POST["name"]; $fname=$_POST["fname"]; $mname=$_POST["mname"]; $address=$_POST["address"]; $email=$_POST["email"]; $r1="insert into registration(name,fathersname,mothersname,address,email)values('$stuname','$fname','$mname','$address','$email')"; $r=mysqli_query($con,$r1); if($r==1) { echo"sucessfull"; } else { echo "error"; } mysqli_close($con); ?>You can Download the volley from this link,click Here.If you didn't understand any concept you cancheck it out my youtube channel sbphptricks.Enjoy Coding.#sbphptricks
Comments
Post a Comment