0

I am working on an android app where I update values in a MySQL database through a php script. I'm using a prepared UPDATE statement. The prepared statement looks like:

$stmt = $conn->prepare("UPDATE qrdata SET height = ?, width = ?, lengthick = ?, denom = 1, des = ?, amount = ? WHERE erp = ?");
$stmt->bind_param("iiisis", $_POST["height"], $_POST["width"], $_POST["length"], $_POST["description"], $_POST["amount"], $_POST["erpcode"]);

In the database: height, width, lengthick, denom, and amount are int values. erp is a varchar, and des is a text value.

Posting the data from my android app was very simple with an AsyncTask. The doInBackground method looks like:

protected String doInBackground(String... strings) {
        String type = strings[0];
        String c = strings[1];
        String h = strings[2];
        String w = strings[3];
        String l = strings[4];
        String n = strings[5];
        String d = strings[6];
        String desc = strings[7];
        String a = strings[8];
        for (int i = 0; i < strings.length; i++) {
            Log.e("Amount", strings[i]);
        }
        String login_url = "http://*****.php";

        if(type.equals("login")) {
            try {
                URL url = new URL(login_url);
                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 post_data = URLEncoder.encode("qrcode","UTF-8")+"="+URLEncoder.encode(c,"UTF-8")+
                        URLEncoder.encode("height", "UTF-8")+"="+URLEncoder.encode(h, "UTF-8")+
                        URLEncoder.encode("length", "UTF-8")+"="+URLEncoder.encode(l, "UTF-8")+
                        URLEncoder.encode("num", "UTF-8")+"="+URLEncoder.encode(n, "UTF-8")+
                        URLEncoder.encode("width", "UTF-8")+"="+URLEncoder.encode(w, "UTF-8")+
                        URLEncoder.encode("desc", "UTF-8")+"="+URLEncoder.encode(desc, "UTF-8")+
                        URLEncoder.encode("den", "UTF-8")+"="+URLEncoder.encode(d, "UTF-8")+
                        URLEncoder.encode("amount", "UTF-8")+"="+URLEncoder.encode(a, "UTF-8");

                bufferedWriter.write(post_data);
                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();
                Log.e("Result of Login",result);
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return "cannot connect";
            } catch (IOException e) {
                e.printStackTrace();
                return "cannot connect";
            }
        }
        return null;
    }

It is called like so:

backgroundworker.execute("login", erp, height, width, length, num, denom, des, amount); 

This is how I usually post data and it has worked for me in plenty of other cases, I also tried changing the first parameter of bind_param to "ssssss" but that also did not work. I'm really not sure why the UPDATE statement is not working, does anyone have any hints?

EDIT : added more error checking as per suggestion

$a = $_POST["den"];
if ($a > 1) {
    $stmt = $conn->prepare("UPDATE qrdata SET height = ?, width = ?, lengthick = ?, denom = ?, des = ?, amount = ? WHERE erp = ?");
    $stmt->bind_param("iiiisis", $_POST["height"], $_POST["width"], $_POST["num"], $_POST["den"], $_POST["desc"],$_POST["amount"], $_POST["qrcode"]);
    $stmt->execute();
    if ($stmt->affected_rows) {
        echo "Code has been activated";
    }
    else {
        echo $conn->error;
    }
} else {
    $stmt = $conn->prepare("UPDATE qrdata SET height = ?, width = ?, lengthick = ?, des = ?, amount = ? WHERE erp = ?");
    $stmt->bind_param("iiisis", $_POST["height"], $_POST["width"], $_POST["length"], $_POST["des"],$_POST["amount"], $_POST["qrcode"]);
    $stmt->execute();
    if ($stmt->affected_rows === 1) {
        echo "Codigo activado";
    }
    else {
        echo $conn->error;
    }
}

I get a notice saying: "Undefined index: den" even though it is very obviously is. What do I do now?

InigoMontoyaJr
  • 101
  • 2
  • 11
  • Are you sure that you are using POST and not get? echo both please – nbk Dec 20 '19 at 23:29
  • Yes, that is in my latest code, I just forgot to change it in my question. I just barely edited it right now. The update statement still does not work – InigoMontoyaJr Dec 20 '19 at 23:34
  • have you an error hasndling im plemnted? it is not visible in your code – nbk Dec 20 '19 at 23:53
  • I error handle within the PHP code by using a simple if statement to check how many rows have been affected and i ```echo``` "not successful" if nothing happens. I then pass what I echoed into an AlertDialog in Android. This way, the error is annoying but not fatal – InigoMontoyaJr Dec 20 '19 at 23:57
  • Please turn on error reporting and then report back if you're not able to resolve it - https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display and also https://phpdelusions.net/mysqli/error_reporting – waterloomatt Dec 21 '19 at 00:10
  • always especially when developing always check the correct mysql error message, only an of clause is not enough – nbk Dec 21 '19 at 00:16
  • I did the suggestion, but Im still nowhere :( – InigoMontoyaJr Dec 21 '19 at 00:49

0 Answers0