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

报告相同问题?

悬赏问题

  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解