Jonathan Perez Jonathan Perez - 3 months ago 22
MySQL Question

Android, php, MySQL, and phpMyAdmin

Hoping you guys can shed some light. I'm working on an Android Capstone project that is connecting to a MySQL DB through php and phpMyAdmin. I'm trying to pass three parameter (childId, date1, and date5) from Android to the php script and it's not working. Logcat just shows Error when I try and print out the result. I think the issue is in my php script because everything works if I only pass 2 parameters (childId and date1). Any help is greatly appreciated. Here is my code.php first:

<?php
require "conn.php";

$response = array();

if($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$childId = $_POST["childId"];
$date1 = $_POST["date"];
$date5 = $_POST["date"];


$sql = "SELECT * FROM child_sessions WHERE childId like '$childId' AND date BETWEEN '$date1' AND '$date5'";


$result = $conn->query($sql);

if($result->num_rows > 0) {

$response["child_sessions"] = array();

while($row = mysqli_fetch_array($result)) {
$child_sessions = array();
$child_sessions["date"] = $row["date"];
$child_sessions["timeIn"] = $row["timeIn"];
$child_sessions["timeOut"] = $row["timeOut"];
$child_sessions["duration"] = $row["duration"];
$child_sessions["childId"] = $row["childId"];
$child_sessions["sessionCost"] = $row["sessionCost"];
array_push($response["child_sessions"], $child_sessions);
}

// success
$response["success"] = 1;

echo json_encode($response);
} else {
echo "Error";
}

$conn->close();
?>


And here's my connection from Android:

private String childSessions(String childId, String date1, String date5) {

InputStream inputStream = null;
String line = null;

Log.d(TAG, "childSessions: " + childId);
Log.d(TAG, "childSessions: " + date1);
Log.d(TAG, "childSessions: " + date5);
try {
String login_url = Constants.RETRIEVE_CHILD_SESSION_FOR_WEEK;
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod(Constants.REQUEST_METHOD);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("childId", "UTF-8") + "=" + URLEncoder.encode(childId, "UTF-8") + "&"+
URLEncoder.encode("date", "UTF-8") + "=" + URLEncoder.encode(date1, "UTF-8") + "&" +
URLEncoder.encode("date", "UTF-8") + "=" + URLEncoder.encode(date5, "UTF-8");
bufferedWriter.write(post_data);

Log.d(TAG, "childSessions: " + post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));

Log.d(TAG, "childSessions: " + bufferedReader);

String result = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
Log.d(TAG, "childSessions: " + result);
}
bufferedReader.close();
//result += System.getProperty("line.separator") + responseOutput.toString();

inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}


Thanks in advance.

Answer

You are encoding a single 'date' parameter twice (and reading it twice in php). Fix your php and java to have the appropriate parameter keys date1 and date5.

so, in php script :

$date1 = $_POST["date1"];
$date5 = $_POST["date5"];

and in java , change the line to :

String post_data = URLEncoder.encode("childId", "UTF-8") + "=" + URLEncoder.encode(childId, "UTF-8") + "&"+
            URLEncoder.encode("date1", "UTF-8") + "=" + URLEncoder.encode(date1, "UTF-8") + "&" +
            URLEncoder.encode("date5", "UTF-8") + "=" + URLEncoder.encode(date5, "UTF-8");
Comments