目前,如果值仅包含单个数据而不是通过循环(如字符串数组),我已经通过将数据包装在Map数据类型中,成功地通过volley网络库发送字符串数据。我已经检查了地图内部的值并且所有数据都是正确的,但是当我尝试通过PHP发送数据时,我在mySQL中得到的东西都没有,在此之前结果只包含值的第一个字母。下面是我通过volley库的POST代码,以及用于从POST代码中获取值的PHP代码。
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";
}
?>