Android response code 200 from php mysql but does not make entries in database

  • Replies:4
  • Answered
Kabir Menk
  • Forum posts: 6

Jun 28, 2016, 11:35:09 PM via Website

I am sending data from my android app form to php mysql server and from server i am getting response code 200 but failed to make any entries into database.

Here is my php Code

$host='localhost';
$uname='root';
$pass='pass';
$db='App';

$con=mysqli_connect($host,$uname,$pass,$db);

$First_Name=$_POST['First_Name'];
$Last_Name=$_POST['Last_Name'];
$Phone=$_POST['Phone'];
$Mail_ID=$_POST['Mail_ID'];
$Password =$_POST['Password'];
$La=$_POST['La'];
$Lo=$_POST['Lo'];
$Ac=$_POST['Ac'];
$Pro=$_POST['Pro'];

$sql="INSERT INTO mobile_App(First_Name,Last_Name,Phone,Mail_ID,Password,La,Lo,Ac,Pro)
VALUES ('$First_Name,$Last_Name,$Phone,$Mail_ID,$Password,$La,$Lo,$Ac,$Pro')";

if(mysqli_query($con,$sql)){
echo 'Data Inserted Successfully';
}
else{
echo 'Try Again';

}
mysqli_close($con);
?>

Here is my JAVA class

public class Sending_Data_To_Server extends AsyncTask{
String Lo,La,Ac,Pro,First_Name,Last_Name,Phone,Mail_ID,Password;
BufferedReader reader;

@Override
protected String doInBackground(String... params) {
    //Variables
    First_Name=params[0];
    Last_Name=params[1];
    Phone=params[2];
    Mail_ID=params[3];
    Password=params[4];
    Lo=params[5];
    La=params[6];
    Ac=params[7];
    Pro=params[8];
    Log.d("SEND DATA VALUES",First_Name+Last_Name+Lo+La+Phone);





    try {
        String data= URLEncoder.encode("First_Name","UTF-8")+ "=" +URLEncoder.encode(First_Name,"UTF-8");
        data+= "&" +URLEncoder.encode("Last_Name","UTF-8")+ "=" +URLEncoder.encode(Last_Name,"UTF-8");
        data+= "&" + URLEncoder.encode("Phone","UTF-8")+ "=" +URLEncoder.encode(Phone,"UTF-8");
        data+= "&" + URLEncoder.encode("Mail_ID","UTF-8")+ "=" +URLEncoder.encode(Mail_ID,"UTF-8");
        data+= "&" + URLEncoder.encode("Password","UTF-8")+ "=" +URLEncoder.encode(Password,"UTF-8");
        data+= "&" + URLEncoder.encode("La","UTF-8")+ "=" +URLEncoder.encode(La,"UTF-8");
        data+= "&" + URLEncoder.encode("Lo","UTF-8")+ "=" +URLEncoder.encode(Lo,"UTF-8");
        data+= "&" + URLEncoder.encode("Ac","UTF-8")+ "=" +URLEncoder.encode(Ac,"UTF-8");
        data+= "&" + URLEncoder.encode("Pro","UTF-8")+ "=" +URLEncoder.encode(Pro,"UTF-8");



        URL url=new URL("test/Android/Data/data.php");   // can't post complete url due to restriction to new users 
        HttpURLConnection connection= (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //For POST Only - Begin
        connection.setDoOutput(true);
        OutputStream os=connection.getOutputStream();
        BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
        writer.write(data);
        writer.flush();
        writer.close();
        os.close();
        connection.connect();
        //For POST Only End
        int responseCode=connection.getResponseCode();
        Log.d("Sending Class----","POST RESPONSE CODE "+responseCode);

        if (responseCode==HttpURLConnection.HTTP_OK){
            //Success
            reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response=new StringBuffer();

            while((inputLine=reader.readLine())!=null){
                response.append(inputLine);
            }
            reader.close();

            //Print Result
            Log.d("SENDING CLASS----",response.toString());

        }
        else{
            Log.d("SENDING CLASS---","POST did not work");

        }


    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


    return "DATA SUBMITTED";
}

}

Please tell me what i am doing wrong in code which not let insert entries into database

Reply
James Watson
  • Forum posts: 1,584

Jun 29, 2016, 2:38:07 AM via Website

Perhaps you need "'" for string value in SQL command?

String data= URLEncoder.encode("First_Name","UTF-8")+ "= '" +URLEncoder.encode(First_Name,"UTF-8") + "' ";

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Reply
Kabir Menk
  • Forum posts: 6

Jun 29, 2016, 10:22:53 AM via Website

Single quote in java is for char not for string.Please help me resolve this problem

Reply
James Watson
  • Forum posts: 1,584

Jun 30, 2016, 4:20:31 AM via Website

I meant that you should check whether you have missed some single quotes or double quotes in either PHP or Java codes for the inserting SQL commands. You may trace and print the created SQL command strings in order to verify they are right.

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Reply
James Watson
  • Forum posts: 1,584

Jul 1, 2016, 3:42:32 AM via Website

As we know, single quotes should be used for string value in SQL command.

For example,

insert into mytable (First_Name,Last_Name,Phone,Mail_ID) VALUES ( 'First_Name', 'Last_Name', '2-872384', 'hello@live.com')

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Kabir Menk

Reply