什么时候在android编程中进行注册服务

时间:2017-03-26 05:55:13

标签: php android

我正在尝试登录,并使用Android Studio和PHP注册系统。 但是,我在制作新会员时遇到了问题。 以下是有关注册服务的源代码。

<?php
	$con = mysqli_connect("localhost", "ekb2011", "*********", "ekb2011");
	$userID=$_POST["postID"];
	$userPassword=$_POST["userPassword"];
	$userName=$_POST["userName"];
	
	$statement=mysqli_prepare($con, "INSERT INTO USER VALUES (?, ?, ?)");
	mysqli_stmt_bind_param($statement, "sss", $userID, $userPassword, $userName);
	mysqli_stmt_execute($statement);
	
	$response=array();
	$response["success"]=true;

	echo json_encode($response);
?>

以下是Android Studio与PHP连接的源代码。

enter code here
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;


public class RegisterRequest extends StringRequest {
    final static private String URL="http://ekb2011.cafe24.com/Register.php";
    private Map<String, String> parameters;
    public RegisterRequest(String userID, String userPassword, String userName, Response.Listener<String> listener){
        super(Method.POST, URL, listener, null);
        parameters=new HashMap<>();
        parameters.put("userID", userID);
        parameters.put("userPassword", userPassword);
        parameters.put("userName", userName);
    }
    @Override
    public Map<String, String> getParams(){
        return parameters;
    }

}

当我注册新成员时,userID,userPassword和userName不会出现在数据库中。 如何修复这些代码才能运行?

1 个答案:

答案 0 :(得分:0)

也许问题是:

$userID=$_POST["postID"];

它应该是:

$userID=$_POST["userID"];

并且最好包含列名并检查是否执行了查询,如下所示:

<?php
$response=array();
$con = mysqli_connect("localhost", "ekb2011", "*********", "ekb2011");
// Check connection
if (!$con) {
    $response["success"] = false;
    $response["error"] = "Connection failed: " . mysqli_connect_error();
    die(json_encode($response));
}


$userID=$_POST["userID"];
$userPassword=$_POST["userPassword"];
$userName=$_POST["userName"];

$statement=mysqli_prepare($con, "INSERT INTO USER (columnId, colunPassword, columnUsername) VALUES (?, ?, ?)");// Change columnId, colunPassword, columnUsername with your table column names
mysqli_stmt_bind_param($statement, "sss", $userID, $userPassword, $userName);


if(mysqli_stmt_execute($statement)){

    $response["success"]=true;
}else{
    $response["success"]=false;
    $response["error"]=mysqli_stmt_error($stmt);
}
echo json_encode($response);
?>