0

Currently, I already successfully send string data through volley networking library by wrapping the data inside Map data type if the value only consist of single data and not through looping such as string array. I already Check the value inside of the map and all of data is correct, but when i try to send the data through PHP, what i got inside mySQL is nothing, and before that the result consist only the first letter of the value. Below is my POST code through volley library, and my PHP code for getting the value from the POST code.

StringRequest stringRequest = new StringRequest(Request.Method.POST,     Register_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            if (response.contains("Success")) {
                Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //Error Based on Volley
            Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show();
        }
    }) {

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> String_Map = new HashMap<String, String>();
            //User Data
            String_Map.put("Name", TIET_Register_Name.getText().toString());
            String_Map.put("Email", TIET_Register_Email.getText().toString());
            String_Map.put("Phone", TIET_Register_Phone.getText().toString());
            String_Map.put("Username", TIET_Register_Email.getText().toString());//Username = Email
            String_Map.put("Password", Password);
            if (ACTV_Parent.isChecked()) {//Parent
                String_Map.put("Status", "Parent");
                String_Map.put("Parent_Status", Spinner_Register_Parent.getSelectedItem().toString());
            } else if (ACTV_Teacher.isChecked()) {//Teacher
                String_Map.put("Status", "Teacher");
            } else {//Staff
                String_Map.put("Status", "Staff");
            }
            int i = 0;
            for (String Child_Name : Container_String) {
                String_Map.put("Child_Name[" + (i++) + "]", Child_Name);
            }
            return String_Map;
        }
    };
    Volley.newRequestQueue(getApplicationContext()).add(stringRequest);

<?php


//User Data
$name = $_POST['Name'];
$email = $_POST['Email'];
$phone = $_POST['Phone'];
$username = $_POST['Username'];
$password = $_POST['Password'];
$status = $_POST['Status'];
$p_status = $_POST['Parent_Status'];

//Child Data

//My suspect the problem lies between these three line. 
$child_name = $_POST['Child_Name'];
$child_bdate = $_POST['Born_Date'];
$child_gstat = $_POST['Graduation_Status'];

require_once('Connect.php');//Connect to Connect.php

//User Insert
$SQL_Insert = "INSERT INTO ms_user(NAME, EMAIL, PHONE, USERNAME, PASSWORD,  STATUS, PARENT_STATUS)
VALUES('$name', '$email', '$phone', '$username', '$password',  '$status', '$p_status')";

if(mysqli_query($Connect, $SQL_Insert)){

//Get user id
$Last_ID = "SELECT MAX(USER_ID) AS Temp_Value FROM ms_user";
$Query = mysqli_query($Connect, $Last_ID);
$Result = mysqli_fetch_assoc($Query);
$Fix = $Result['Temp_Value'];
}

//Insert child data
for($i = 0;$i<count($child_name);$i++){
$Child_SQL_Insert = "INSERT INTO ms_child(CHILD_NAME, BORN_DATE, GRAD_STAT, PARENT_ID)
VALUES('$child_name[i]', '$child_bdate[i]', '$child_gstat[i]', '$Fix')";
}
if(mysqli_query($Connect, $Child_SQL_Insert)){
    echo "Success";
}
else{
    echo "Failed";
}
?>

This is the result looks like in MySQL

  • You are open to SQL injection attacks. Learn about prepared statements. http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – Jeremy Harris Dec 22 '16 at 18:16
  • Thank you for your advice, but do you have any idea in PHP how to get the value from Map data type which is consist on one of the value is string array? – Andre Sumantri Dec 22 '16 at 18:20
  • Did you already debug the $_POST? (var_dump($_POST) or print_r($_POST)). – Asfo Dec 22 '16 at 22:05
  • I just debug it and i the value is successfully passed. Tbh i just know that print_r function. Another problem is how to extract that array value to string and insert it according to the array position. – Andre Sumantri Dec 23 '16 at 01:55

0 Answers0