- Forum posts: 6
Jun 28, 2016, 11:35:09 PM via Website
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
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.