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){
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题