Home Android MySQL : Insert From EditText,CheckBox,Spinner – Part 4 [PHP Code]

Android MySQL : Insert From EditText,CheckBox,Spinner – Part 4 [PHP Code]

Android MySQL : Insert From EditText,CheckBox,Spinner – Part 4 [PHP Code]


Rating : 5/5 (1)


Hello guys.Here's the PHP server side code.

Series

  1. Part 1 - Introduction.
  2. Part 2 - Java Code
  3. Part 3 - XML Layouts.
  4. Part 4 - PHP Code

 

Constants.php Class

  • Holds the database constants like database name,database password and host.
  • We also define our select statement here.

 

<?php

/**
 * Created by Oclemy for ProgrammingWizards and http://www.camposha.com.
 * User: Oclemy
 * Date: 9/16/2016
 * Time: 12:54 AM
 */
class Constants
{
    //DATABASE DETAILS
    static $DB_SERVER="localhost";
    static $DB_NAME="galileoDB";
    static $USERNAME="root";
    static $PASSWORD="";
    const TB_NAME="galileoTB";

    //STATEMENTS
    static $SQL_SELECT_ALL="SELECT * FROM galileoTB";


}

 

 

DBAdapter Class

  • Performs all CRUD operations.
  • Inserts data to MySQL Database.

 

<?php

/**
 * Created by Oclemy for ProgrammingWizards and http://www.camposha.com.
 * User: Oclemy
 * Date: 9/16/2016
 * Time: 12:54 AM
 */
require_once("/Constants.php");

class DBAdapter
{
/*******************************************************************************************************************************************/
/*
   1.CONNECT TO DATABASE.
   2. RETURN CONNECTION OBJECT
*/
    public function connect()
    {
       

       $con=mysqli_connect(Constants::$DB_SERVER,Constants::$USERNAME,Constants::$PASSWORD,Constants::$DB_NAME);

        if(mysqli_connect_error(!$con))
        {
           // echo "Unable To Connect";
            return null;
        }else
        {
           
            return $con;
        }
    }
  /*******************************************************************************************************************************************/
/*
   1.INSERT SPACECRAFT INTO DATABASE
 */
    public function insert($s)
    {
        // INSERT
        $con=$this->connect();
        
        if($con != null)
        {
            $sql="INSERT INTO galileoTB(name,propellant,technologyexists) VALUES('$s[0]','$s[1]','$s[2]')";

            try
            {
                $result=mysqli_query($con,$sql);

                if($result)
                {
                    print(json_encode(array("Success")));
                }else
                {
                    print(json_encode(array("Unsuccessfull")));
                }
            }catch (Exception $e)
            {
               print(json_encode(array("PHP EXCEPTION : CAN'T SAVE TO MYSQL. "+$e->getMessage())));
            }


        }else{
            print(json_encode(array("PHP EXCEPTION : CAN'T CONNECT TO MYSQL. NULL CONNECTION.")));
        }

        mysqli_close($con);
    }
/*******************************************************************************************************************************************/
/*
   1.SELECT FROM DATABASE.
*/
    public function select()
    {
       $con=$this->connect();

        if($con != null)
        {
            $retrieved=mysqli_query($con,Constants::$SQL_SELECT_ALL);
            if($retrieved)
            {
                while($row=mysqli_fetch_array($retrieved))
                {
                   
                   // echo $row["name"] ,"    \t | ",$row["propellant"],"</br>";
           $spacecrafts[]=$row;
                }
        print(json_encode($spacecrafts));
            }else
            {
                 print(json_encode(array("PHP EXCEPTION : CAN'T RETRIEVE FROM MYSQL. ")));
            }
        }else{
            print(json_encode(array("PHP EXCEPTION : CAN'T CONNECT TO MYSQL. NULL CONNECTION.")));
        }

        mysqli_close($con);
    }
/*******************************************************************************************************************************************/
/*
   1.UPDATE  DATABASE.
  
*/
    public function update($id, $s)
    {
        // UPDATE

        $con=$this->connect();
      
        if($con != null)
        {

            $sql="UPDATE galileoTB SET name='$s[0]',propellant='$s[1]',technologyexists='$s[2]' WHERE id='$id'";


            try
            {
                $result=mysqli_query($con,$sql);


                if($result)
                {
                    print(json_encode(array("Successfully Updated")));
                }else
                {
                    print(json_encode(array("Not Successfully Updated")));
                }
            }catch (Exception $e)
            {
                 print(json_encode(array("PHP EXCEPTION : CAN'T UPDATE INTO MYSQL. "+$e->getMessage())));
            }


        }else{
            print(json_encode(array("PHP EXCEPTION : CAN'T CONNECT TO MYSQL. NULL CONNECTION.")));
        }

        mysqli_close($con);
    }
/*******************************************************************************************************************************************/
/*
   1.DELETE SPACECRAFT FROM DATABASE.
  
*/
    public function delete($id)
    {
        $con=$this->connect();
       
        if($con != null)
        {

//            $name=$_POST['Name'];
//            $pos=$_POST['Position'];
//            $team=$_POST['Team'];

            $sql="DELETE FROM galileoTB WHERE id='$id'";


            try
            {
                $result=mysqli_query($con,$sql);


                if($result)
                {
                    print(json_encode(array("Successfully Deleted")));
                }else
                {
                    print(json_encode(array("Not Successfully Deleted")));
                }
            }catch (Exception $e)
            {
                print(json_encode(array("PHP EXCEPTION : CAN'T DELETE FROM MYSQL. "+$e->getMessage())));
            }


        }else{
            print(json_encode(array("PHP EXCEPTION : CAN'T CONNECT TO MYSQL. NULL CONNECTION.")));
        }

        mysqli_close($con);
    }

   
}
p

 

CRUD.php Class

  • Receives the HTTP request from the client,in this case android.
  • It then determines the action to take,whether to call save method or whatever.

 

<?php

/**
 * Created by Oclemy for ProgrammingWizards and http://www.camposha.com.
 * User: Oclemy
 * Date: 9/16/2016
 * Time: 12:54 AM
 */
require_once("/DBAdapter.php");

if($_POST['action']=="save"){
        
   		 $dbAdapter=new DBAdapter();
         $name=$_POST['name'];
         $propellant=$_POST['propellant'];
         $technologyexists=$_POST['technologyexists'];

        $dbAdapter->insert(array($name,$propellant,$technologyexists));
}
else if($_POST['action']=="update")
{
       $dbAdapter=new DBAdapter();
     $id=$_POST['id'];
         $name=$_POST['name'];
         $propellant=$_POST['propellant'];
         $technologyexists=$_POST['technologyexists'];

        $dbAdapter->update($id,array($name,$propellant,$technologyexists));
  
}
else if($_POST['action']=="delete")
{
       $dbAdapter=new DBAdapter();
         $id=$_POST['id'];
        
        $dbAdapter->delete($id);
}
?>

 

 

Index.php File

  • Executes the retrieve/select function to retrieve data from database.

 

<?php
/**
 * Created by Oclemy for ProgrammingWizards and http://www.camposha.com.
 * User: Oclemy
 * Date: 9/16/2016
 * Time: 12:54 AM
 */
require_once("/DBAdapter.php");

  $dbAdapter=new DBAdapter();
  $dbAdapter->select();

?>

 

 

 

Rating :

Leave a Reply

Your email address will not be published. Required fields are marked *

fifteen + sixteen =

2 comments

Ubba March 8, 2017 at 10:22 pm

Hi!
In this video https://www.youtube.com/watch?v=izn8cPAJpao&t=4s minute18 sec 06 of the video, you talk about an AndroidNetworing.class, tell me please, where did you do input te androidNetworking.class??
I’m doing every step of your code, but I have thir error

Reply
Oclemy March 9, 2017 at 4:45 am

Hey,am sorry I didn’t provide proper source code reference link.In the video we are using a library called AndroidNetworking library.We fetch it via our gradle scripts,here is the direct download link for source code : https://github.com/Oclemy/MySQL-DataTypes-Save/archive/master.zip

Reply

Leave a Comment

2 × 3 =