Insert MYSQL || Insert data into MYSQL using PHP

 

Android Insert MYSQL :

This tutorial android insert MYSQL deals with the PHP integrating MYSQL database to insert a values using api and this api is called from android app.

Before going any further let us know the basic operations we can perform like CRUD operations

1) Create

2) Read

3) Update

4) Delete

 

So let’s get started now with android insert MYSQL tutorial

 

Step 1:

Connect.php

This is first step of connecting to the database here we provide the details required

 

  1.  Hostname is the place where the database is.
  2.  Username for the user access.
  3.  Password  is required for maintaining security for local host this will be empty.
  4.  DBName  this will select the database out of all your databases present on your server

 

And can be defined as

define('Hostname','localhost'); //db host
define('Username','root'); // db username
define('Password',''); //db password
define('DBName','userS'); // db name

 

and finally using all these values we will try to connect with server to establish a connection first leter on we can perform fetch, insert, update or delete.

 

$con = mysqli_connect(Hostname,Username,Password,DBName)

 

Step 2:

Insert.php

The first step in insert.php is to connect to a server so

require_once('connect.php');

 

And then accept the values thus coming from api and save them in local variables

here the local variables are  –> $name , $age, $phone, $email

 

$name = $_POST['name'];
$age = $_POST['age'];
$phone = $_POST['phone'];
$email = $_POST['email'];

 

Now run the MYSQL query for inserting the values

 

"insert into user_table (name, age, phone, email) values ('$name','$age','$phone','$email')"

 

Now lets query with the help of connect class and the query written above

$res = mysqli_query($con,$sql);

 

then after processing it will return success or failure and again this will have many barriers to be passed like

  1. Database should exists
  2. Table should be created and
  3. Field name should be matched exactly

 

check condition to finalize the result using a array (array may not be required)

$check = mysqli_fetch_array($res);

 

then a if else condition to validate result

if(isset($check)){
echo 'success';
}else{
echo 'failure';
}

 

Step 3:

At last close the connection i am mentioning this as a step because its important to do so and good practice too.

mysqli_close($con);

 

insert.php

This page helps us to insert MySql data.

<?php

require_once('connect.php');

$name = $_POST['name'];
$age = $_POST['age'];
$phone = $_POST['phone'];
$email = $_POST['email'];

$sql = "insert into user_table (name, age, phone, email)
values ('$name','$age','$phone','$email')";

$res = mysqli_query($con,$sql);

$check = mysqli_fetch_array($res);

if(isset($check)){
echo 'success';
}else{
echo 'failure';
}


mysqli_close($con);
?>

 

If you have any query’s on this tutorial on android insert MySql do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates.

For more detailed explanation on android insert MYSQL view the video tutorial below.

 

 

 

 

 

Show Buttons
Hide Buttons
Read previous post:
Android basic operators || Kotlin Tutorial on Operators

  Android basic operators : In this part of the tutorial we will be dealing with android basic operators like...

Close