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?