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

  • Replies:4
  • OpenNot stickiedAnswered
  • 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

  • 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

  • Forum posts: 256

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.