【发布时间】:2019-12-09 12:56:02
【问题描述】:
我正在开发 android 应用程序并使用此脚本将用户添加到 MySQL 数据库问题是当我在 postman 和 android 应用程序上测试脚本时,我无法将用户添加到数据库,响应总是:
{"OKCode":0,"message":"必填字段缺失"}
这里是 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);
}`
【问题讨论】:
-
PHP 代码应该在此处,而不是在链接页面上。输出你的
POST,看看你实际拥有什么。isset也可以取多个值,你不应该有多个这样的调用。 -
@user3783243 像这样 (isset($_POST['student_name'],$_POST['id_school'],$_POST['id_school'],$_POST['u_password'],$_POST[ 'id_number'],$_POST['licence'],$_POST['phone'],$_POST['username'])))
-
既然您在邮递员内部也有同样的问题,那么问题就出在您的 PHP 代码中。你能用 var_dump($_POST) 看看里面是什么吗?
-
@catcon array(0) { } {"success":0,"message":"Required field(s) is missing"} 并且当将 $_POST 更改为 $_GET 它在邮递员中工作但在android中没有
-
您正在将值添加到查询参数中,但在 POST 请求中,您必须将它们添加到正文中。
标签: java php android api android-volley