dongxian3574 2018-07-30 23:01
浏览 51
已采纳

我是否需要远程登录才能通过php脚本从MySQL数据库读取和写入?

I am practicing creating an authentication system using Java, PHP, and SQL. I have one entry in the MySQL database, but my application refuses to sign in. I also created a register side of the application but it doesn't work either. So I was wondering if not having remote login access would affect it.

if ($_SERVER['REQUEST_METHOD']=='POST') {

$matriculation_number = $_POST['matriculation_number'];
$password = $_POST['password'];

$conn = mysqli_connect("localhost", "id4922470_public", "password", "id4922470_cuc_general");

if ($conn = mysqli_connect("localhost", "id4922470_public", "password", "id4922470_cuc_general")) {
    $sql = "SELECT * FROM users_general WHERE matriculation_number='$matriculation_number' ";

    $response = mysqli_query($conn, $sql);
    $check = array();
    $result = array();
    $check['connection'] = array();
    $result['login'] = array();

    if ( mysqli_num_rows($response) === 1 ) {

        $row = mysqli_fetch_assoc($response);

        if ( password_verify($password, $row['password']) ) {

            $index['matriculation_number'] = $row['matriculation_number'];
            $index['first_name'] = $row['first_name'];
            $index['middle_name'] = $row['middle_name'];
            $index['last_name'] = $row['last_name'];
            $index['gender'] = $row['gender'];
            $index['level'] = $row['level'];
            $index['programme'] = $row['programme'];
            $index['department'] = $row['department'];
            $index['college'] = $row['college'];

            array_push($result['login'], $index);

            $result['success'] = "1";
            $result['message'] = "success";
            echo json_encode($result);

            mysqli_close($conn);

        } else {

            $result['success'] = "0";
            $result['message'] = "error";
            echo json_encode($result);

            mysqli_close($conn);

        }

    } else {
        $check['Connected'] = "Connection failed";
        echo json_encode($check);
    }
}
}

The volley code:

StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_REQUEST_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        System.err.println("__________________________________________LOG________________________________________");
                        Log.i("tagconvertstr", "["+response+"]");
                        System.err.println("__________________________________________LOG________________________________________");
                        JSONObject jsonObject = new JSONObject(response);
                        String success = jsonObject.getString("success");
                        JSONArray jsonArray = jsonObject.getJSONArray("login");

                        if (success.equals("1")) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject object = jsonArray.getJSONObject(i);

                                String matriculation_number = object.getString("matriculation_number").trim();
                                String first_name = object.getString("first_name").trim();
                                String middle_name = object.getString("middle_name").trim();
                                String last_name = object.getString("last_name").trim();
                                String gender = object.getString("gender").trim();
                                int level = object.getInt("level");
                                String programme = object.getString("programme").trim();
                                String department = object.getString("department").trim();
                                String college = object.getString("college").trim();

                                System.out.println(matriculation_number + "
" + first_name + "
" + middle_name + "
" +
                                last_name + "
" + gender + "
" + level + "
" + programme + "
" +department + "
" + college);
                                System.out.println("___________________________CHECK___________________________________");

                                String message = "Welcome " + matriculation_number;
                                Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();

                                Intent l = new Intent(LoginActivity.this, DashboardActivity.class);
                                startActivity(l);
                                LoginActivity.this.finish();
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        String message = "Error " + e.toString();
                        Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
                        pWord.setText("");
                        pWord.clearFocus();
                        uName.clearFocus();
                        login.setVisibility(View.VISIBLE);
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    String message = "Error " + error.toString();
                    Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
                    pWord.setText("");
                    pWord.clearFocus();
                    uName.clearFocus();
                    login.setVisibility(View.VISIBLE);
                }
            }
    ) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("matriculation_number", matriculation_number);
            params.put("password", password);
            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

I was asked to post the script... which I have done. I have also added a check to make sure the script is connected to the database.

  • 写回答

1条回答 默认 最新

  • douzhan1963 2018-07-30 23:07
    关注

    since the php file is responsible for connect to the database and look for use validation java doesn't have to have access for db for this to work. all you need to do is calling the php endpoint from java .. use some http library to do the post to the php file.

    first see if the php file works right manually. you can send a post to the php file using postman. https://www.getpostman.com/docs/v6/postman/sending_api_requests/requests

    then look at the output. if its ok then there may be an error in your java code.

    and i think this line of code wont work

    if ($conn = mysqli_connect("localhost", "id4922470_public", "password","id4922470_cuc_general")) {
    
    }
    

    should replace with

    if($conn){
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮