-1

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);
?>

And this is the screenshot of my data base table: enter image description here

Naveen Kumar
  • 160
  • 1
  • 10
RickyPang
  • 55
  • 1
  • 8
  • Did you mean to include your server password? If it’s real, you should change it right away (and pick a stronger one). Also, please read about SQL injection, because you’re vulnerable: https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Ry- Mar 28 '19 at 03:26

3 Answers3

0

You have an error in your mysql query. You have only 5 columns in your fields, but you are passing 6 fields.

$sql = "INSERT INTO projectData(customerName, carName, appointmentDate, email, issueDescribe)
VALUES ('$cuName', '$caName', '$appDate', '$ema', '$isDescribe', '$tiJob')"; 

$tiJob is the extra field.

Naveen Kumar
  • 160
  • 1
  • 10
0
  1. I think that you use Postman to test the POST, make sure that the PHP side works OK.
  2. It seems that the "insert into" clause is invalid.
Vinh Can Code
  • 407
  • 3
  • 14
0

check your code; this line of insert:

$sql = "INSERT INTO projectData(customerName, carName, appointmentDate, email, issueDescribe)
VALUES ('$cuName', '$caName', '$appDate', '$ema', '$isDescribe', '$tiJob')"; 

have 5 column in the structure:

customerName, carName, appointmentDate, email, issueDescribe

And you send 6 variable to populate the table.

'$cuName', '$caName', '$appDate', '$ema', '$isDescribe', '$tiJob'

i recommend you to migrate to PDO instead of use mysqli