drjmrg8766 2016-04-13 13:47
浏览 49

要插入的PHP脚本只是返回<br />而不是将数据插入数据库[重复]

I'm using android studio and connecting to a database. I've managed to retrieve information from the database and created a log in feature which is working fine. I'm now trying to work on creating a register feature. To register the user only needs to enter their forename, surname, username and password. I've been trying to follow a tutorial from simplified coding but for some reason I can't get it to work. In the code a toast is being used to display an error message if one is returned from the php script but with my code the toast message just displays
This is the php script I'm using for this feature

<?php

 $forename = $_GET['forename'];
 $surname = $_GET['surname'];
 $username = $_GET['username'];
 $password = $_GET['password'];
 $dob = $_GET['dob'];

 if($forename == '' || $surname == '' || $username == '' || $password == '' || $dob == ''){
 echo 'please fill all values';
 }else{
 require_once('dbConnect.php');
 $sql = "SELECT * FROM users WHERE username='$username'";

 $check = mysqli_fetch_array(mysqli_query($con,$sql));

 if(isset($check)){
 echo 'username already exist';
 }else{ 
 $sql = "INSERT INTO users (forename,surname,username,password,dob) VALUES('$forename','$surname','$username','$password','$dob')";
 if(mysqli_query($con,$sql)){
 echo 'successfully registered';
 }else{
 echo 'oops! Please try again!';
 }
 }
 mysqli_close($con);
 }
?>

And this is the relevant parts of my java code within the application

@Override
    public void onClick(View v){
        if(v == buttonReg){
            registerUser();
        }
    }

 public void registerUser(){

        String forename = eTxtForename.getText().toString().trim().toLowerCase();
        String surname = eTxtSurname.getText().toString().trim().toLowerCase();
        String username = eTxtUserName.getText().toString().trim().toLowerCase();
        String password = eTxtPass.getText().toString().trim().toLowerCase();

        register(forename, surname, username, password);
    }
private void register(String forename, String surname, String username, String password){
        String urlSuffix = "?forename="+forename+"&surname="+surname+"&username="+username+"&password="+password;
        class RegisterUser extends AsyncTask<String, Void, String>{

            ProgressDialog loading;

            @Override
            protected void onPreExecute(){
                super.onPreExecute();
                loading = ProgressDialog.show(RegisterActivity.this, "Please Wait", null, true, true);
            }

            @Override
            protected void onPostExecute(String s){
                super.onPostExecute(s);
                loading.dismiss();
                Log.d("From s", s);
                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
            }

            @Override
            protected String doInBackground(String... params){
                String s = params[0];
                BufferedReader bufferedReader = null;
                try{
                    URL url = new URL(Config.URL_REGISTER+s);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                    String result;
                    result = bufferedReader.readLine();

                    return result;
                }catch (Exception e){
                    return null;
                }
            }
        }

        RegisterUser ru = new RegisterUser();
        ru.execute(urlSuffix);

    }

I think that's everything that you will need to see, but let me know if I need to include anything else

EDIT: As suggested I've used HttpRequester to send a get request to see what the error I'm getting is. The error says:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /home/u613295501/public_html/register.php on line 14

so line 14 is this line

$check = mysqli_fetch_array(mysqli_query($con,$sql));
</div>
  • 写回答

2条回答 默认 最新

  • dqz86173 2016-04-13 13:54
    关注

    The
    being displayed is a php error you will have to use a get request sender....like post man in chrome store to send a dummy get request and check the full error...hope i helped

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错