I want to insert new data in my android application, the data will be sent to MYSQL workbench database. But when I try to edit the new data and click save button, the data will not go into the database. It got the input result when I try to print it out. I using PHP file to let the application to connect to the database. Is there anything I miss for setting up the method?
There are no error on my code and Logcat. And I should those line when I click the save button:
03-28 02:58:07.697 4985-5433/advprog.mmu.ac.uk.projectapp I/System.out: Test:customerName=test&carName=test&appointmentDate=tte&email=testes&issueDescribe=tstes&timeForJob=testes
03-28 02:58:13.597 4985-5433/advprog.mmu.ac.uk.projectapp W/zygote: Verification of void com.android.tools.profiler.support.network.HttpTracker$InputStreamTracker.(java.io.InputStream, com.android.tools.profiler.support.network.HttpTracker$Connection) took 143.013ms
03-28 02:58:15.090 4985-5031/advprog.mmu.ac.uk.projectapp D/EGL_emulation: eglMakeCurrent: 0x9e4afd00: ver 3 0 (tinfo 0x9d53ea40)
03-28 02:58:15.285 4985-5031/advprog.mmu.ac.uk.projectapp D/EGL_emulation: eglMakeCurrent: 0x9e4afd00: ver 3 0 (tinfo 0x9d53ea40)
03-28 02:58:16.752 4985-4993/advprog.mmu.ac.uk.projectapp I/zygote: Do full code cache collection, code=244KB, data=163KB After code cache collection, code=244KB, data=133KB
It get the value from the EditText but just doesn't pass to database.
The is the Java Code in the application:
public class BackgroundWorker extends AsyncTask<String, Void, String> implements advprog.mmu.ac.uk.projectapp.BackgroundWorker {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx) {
context = ctx;
}
protected String doInBackground(String... params) {
String newUrl = "http://10.0.2.2:8080/projectWeb/insert.php";
String customerName = params[0];
String carName = params[1];
String appointmentDate = params[2];
String email = params[3];
String issueDescribe = params[4];
String timeForJob = params[5];
try {
URL url = new URL(newUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String postData = URLEncoder.encode("customerName", "UTF-8") + "=" + URLEncoder.encode(customerName, "UTF-8") +"&"
+ URLEncoder.encode("carName", "UTF-8") + "=" + URLEncoder.encode(carName, "UTF-8") +"&"
+ URLEncoder.encode("appointmentDate", "UTF-8") + "=" + URLEncoder.encode(appointmentDate, "UTF-8") +"&"
+ URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") +"&"
+ URLEncoder.encode("issueDescribe", "UTF-8") + "=" + URLEncoder.encode(issueDescribe, "UTF-8") +"&"
+ URLEncoder.encode("timeForJob", "UTF-8") + "=" + URLEncoder.encode(timeForJob, "UTF-8");
System.out.println("Test:" + postData);
bufferedWriter.write(postData);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
PHP file:
<? php
$servername = "mudfoot.doc.stu.mmu.ac.uk:6306";
$username = "username";
$password = "password";
$dbname = "pangh";
$dbconn = mysqli_connect($servername, $username, $password, $dbname);
$cuName = $_POST["customerName"];
$caName = $_POST["carName"];
$appDate = $_POST["appointmentDate"];
$ema = $_POST["email"];
$isDescribe = $_POST["issueDescribe"];
$tiJob = $_POST["timeForJob"];
$sql = "INSERT INTO projectData(customerName, carName, appointmentDate, email, issueDescribe)
VALUES ('$cuName', '$caName', '$appDate', '$ema', '$isDescribe', '$tiJob')";
if($dbconn->query($sql) === TRUE) {
echo "Insert Successful";
} else {
echo "Error: " .$sql . "<br>" .$dbconn->error;
}
mysqli_close($dbconn);
?>