我正在开发android应用,并使用此脚本将用户添加到MySQL数据库,问题是当我在邮递员和android应用上测试脚本时,我无法将用户添加到数据库中,响应始终是:
{“ OKCode”:0,“消息”:“必填字段丢失”}
这是php脚本:
<?php
header('Content-Type: text/html; charset=utf-8');
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// check for post data
if (
isset($_POST['student_name']) &&
isset($_POST['id_school']) &&
isset($_POST['u_password']) &&
isset($_POST['id_number']) &&
isset($_POST['licence']) &&
isset($_POST['phone'])&&
isset($_POST['username'])
) {
$student_name = $_POST['student_name'];
$id_school = $_POST['id_school'];
$u_password = $_POST['u_password'];
$id_number = $_POST['id_number'];
$licence = $_POST['licence'];
$phone = $_POST['phone'];
$username = $_POST['username'];
$id ;
// connecting to db
$db = new DB_CONNECT();
$con = $db->_connect();
if (!userExists($con, $username)) {
if (insertIntoUser($con, $username, $id_school, $u_password)) {
# when the student insert into user add The student to Student
table
# whit the last update id
if (insertIntoStudent($con, $id, $id_school, $student_name, $licence, $id_number, $phone)) {
$response["OKCode"] = 1;
$response["message"] = "done";
} else {
$response["OKCode"] = 0;
$response["message"] = "fail";
}
} else {
$response["OKCode"] = 0;
$response["message"] = "fail";
}
}else {
$response["OKCode"] = 0;
$response["message"] = "This account is already in users";
}
}else {
$response["OKCode"] = 0;
$response["message"] = "Required field(s) is missing";
}
echo json_encode($response, JSON_UNESCAPED_UNICODE);
/* check if username is not in the user tabel for this
username is the id_number of the student
*/
function userExists($con, $username)
{
$query = "SELECT username FROM users WHERE username = ?";
if ($stmt = $con->prepare($query)) {
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
if ($stmt->num_rows == 1) {
$stmt->close();
return true;
}
$stmt->close();
}
return false;
}
function insertIntoUser($con, $username, $id_school, $u_password)
{
$account_type = 2;
$insertQuery = "INSERT INTO users (id_school, username, u_password,
account_type) VALUES (?,?,?,?)";
$stmt = $con->prepare($insertQuery);
if (
$stmt &&
$stmt->bind_param("isii", $id_school, $username, $u_password,
$account_type) &&
$stmt->execute()
) {
global $id;
$id = $stmt->insert_id;
$stmt->close();
return true;
}
$stmt->close();
return false;
}
function insertIntoStudent($con, $id, $id_school, $student_name, $licence,
$id_number, $phone)
{
$insertQuery = "INSERT INTO student(id, id_school, student_name, licence,
id_number, phone) VALUES (?,?,?,?,?,?)";
$stmt = $con->prepare($insertQuery);
if (
$stmt && $stmt->bind_param("iissii", $id, $id_school, $student_name,
$licence, $id_number, $phone) &&
$stmt->execute()
) {
return true;
$stmt->close();
}
$stmt->close();
return false;
}
邮递员的屏幕截图
,这是Java代码`
私人无效addNewStudent(){
StringRequest request = new StringRequest(Request.Method.POST,
ADD_NEW_STUDENT_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
pBar.setVisibility(View.GONE);
btnAddStudent.setVisibility(View.VISIBLE);
isSelected = false;
radioGroup.clearCheck();
Log.d("MyTag", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() {
Map<String,String> stringMap = new HashMap<>();
stringMap.put("student_name",studentName);
stringMap.put("id_school",idSchool);
stringMap.put("u_password",password);
stringMap.put("id_number",idNumber);
stringMap.put("licence",type);
stringMap.put("phone",phoneNumber);
stringMap.put("username","hp");
return stringMap;
}
};
AppController.getInstance().addToRequestQueue(request,ADD_STUDENT_TAG);
}`
答案 0 :(得分:-2)
响应始终为:{“ OKCode”:0,“ message”:“必填字段丢失”}
if (
isset($_POST['student_name']) &&
isset($_POST['id_school']) &&
isset($_POST['u_password']) &&
isset($_POST['id_number']) &&
isset($_POST['licence']) &&
isset($_POST['phone'])&&
isset($_POST['username'])
) {
# (..)
}else {
$response["OKCode"] = 0;
$response["message"] = "Required field(s) is missing"; # <----
}
因此您的$ _POST字段中的一个或多个不存在。尝试使用print_r($_POST)
来查看POST中的内容。