2016-07-28 62 views
0

我一直在试图为我的应用程序的用户注册表格,但我似乎无法注册新用户到我的数据库。编译时没有错误,甚至不显示错误对话框。我很喜欢PHP和MYSQLI。帮助将不胜感激。 PS我正在使用Volley。无法注册用户到数据库。没有错误

这里是我的Register.java代码:

public class Register extends AppCompatActivity { 


Button btnRegister; 

EditText UserName, Name, Surname, Email, Bdate, Bmonth, Byear, HouseNr, StreetName, CityName, 
     PostalCode, CountryName, CountryMobileCode, Mobile, Password, ConPassword; 

String userName, name, surname, email, bdate, bmonth, byear, houseNr, streetName, cityName, 
     postalCode, countryName, countryMobileCode, mobile, password, conPassword; 

String reg_url = "http://xxxxxxxxxx/register.php"; 

String birthday, address, userMobile; 

AlertDialog.Builder builder; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_register); 

    btnRegister = (Button) findViewById(R.id.btnDetailsConfirm); 

    UserName = (EditText) findViewById(R.id.etUsername); 
    Name = (EditText) findViewById(R.id.etName); 
    Surname = (EditText) findViewById(R.id.etSurname); 
    Email = (EditText) findViewById(R.id.etEmail); 
    Bdate = (EditText) findViewById(R.id.etBday); 
    Bmonth = (EditText) findViewById(R.id.etBmonth); 
    Byear = (EditText) findViewById(R.id.etByear); 
    HouseNr = (EditText) findViewById(R.id.etHouseNr); 
    StreetName = (EditText) findViewById(R.id.etStreetName); 
    CityName = (EditText) findViewById(R.id.etCity); 
    PostalCode = (EditText) findViewById(R.id.etPostalCode); 
    CountryName = (EditText) findViewById(R.id.etCountry); 
    CountryMobileCode = (EditText) findViewById(R.id.etMobileExt); 
    Mobile = (EditText) findViewById(R.id.etMobileNr); 
    Password = (EditText) findViewById(R.id.etPassword); 
    ConPassword = (EditText) findViewById(R.id.etPasswordCon); 

    builder = new AlertDialog.Builder(Register.this); 

    btnRegister.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      userName = UserName.getText().toString(); 
      name = Name.getText().toString(); 
      surname = Surname.getText().toString(); 
      email = Email.getText().toString(); 
      bdate = Bdate.getText().toString(); 
      bmonth = Bmonth.getText().toString(); 
      byear = Byear.getText().toString(); 
      houseNr = HouseNr.getText().toString(); 
      streetName = StreetName.getText().toString(); 
      cityName = CityName.getText().toString(); 
      postalCode = PostalCode.getText().toString(); 
      countryName = CountryName.getText().toString(); 
      countryMobileCode = CountryMobileCode.getText().toString(); 
      mobile = Mobile.getText().toString(); 
      password = Password.getText().toString(); 
      conPassword = ConPassword.getText().toString(); 


      birthday = bdate+"/"+bmonth+"/"+byear; 
      address = houseNr+" "+streetName+", "+cityName; 
      userMobile = countryMobileCode+mobile; 

      if(userName.equals("")||name.equals("")||surname.equals("")||email.equals("")||bdate.equals("")||bmonth.equals("")||byear.equals("")|| 
        houseNr.equals("")||streetName.equals("")||cityName.equals("")||postalCode.equals("")||countryName.equals("")||countryMobileCode.equals("")|| 
        mobile.equals("")||password.equals("")||conPassword.equals("")) 
      { 
       builder.setTitle("Something has gone wrong..."); 
        builder.setMessage("Please fill in all the fields."); 
        displayAlert("input_error"); 
      } 

      else 
      { 
       if(!(password.equals(conPassword))) 
       { 
        builder.setTitle("Something has gone wrong..."); 
        builder.setMessage("Passwords do not match."); 
        displayAlert("input_error"); 
       } 
       else 
       { 
        StringRequest stringRequest = new StringRequest(Request.Method.POST, reg_url, 
          new Response.Listener<String>() { 
           @Override 
           public void onResponse(String response) { 

            try { 
             JSONArray jsonArray = new JSONArray(response); 
             JSONObject jsonObject = jsonArray.getJSONObject(0); 
             String code = jsonObject.getString("code"); 
             String message = jsonObject.getString("message"); 
             builder.setTitle("Server Response..."); 
             builder.setMessage(message); 
             displayAlert(code); 

            } catch (JSONException e){ 
             e.printStackTrace(); 
            } 
           } 
           }, new Response.ErrorListener() 

           { 
            @Override 
            public void onErrorResponse(VolleyError error){ 

           } 
           }){ 
         @Override 
         protected Map<String,String> getParams() throws AuthFailureError { 
          Map<String, String> params = new HashMap<String,String>(); 

          params.put("user_name", userName); 
          params.put("name", name); 
          params.put("surname", surname); 
          params.put("email", email); 
          params.put("birthday", birthday); 
          params.put("address", address); 
          params.put("postal_code", postalCode); 
          params.put("city", cityName); 
          params.put("country", countryName); 
          params.put("mobile", userMobile); 
          params.put("password", password); 


          return params; 
         } 
        }; 

        MySingleton.getInstance(Register.this).addToRequestQue(stringRequest); 

       } 
      } 



     } 
    }); 
} 

public void displayAlert(final String code) 
{ 
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 

      if (code.equals("input_error")) 
      { 
       Password.setText(""); 
       ConPassword.setText(""); 
      } 
      else if (code.equals("reg_success")) 
      { 
       finish(); 
      } 

      else if(code.equals("reg_failed")) 
      { 
       UserName.setText(""); 
       Name.setText(""); 
       Surname.setText(""); 
       Email.setText(""); 
       Bdate.setText(""); 
       Bmonth.setText(""); 
       Byear.setText(""); 
       HouseNr.setText(""); 
       StreetName.setText(""); 
       CityName.setText(""); 
       PostalCode.setText(""); 
       CountryName.setText(""); 
       CountryMobileCode.setText(""); 
       Mobile.setText(""); 
       Password.setText(""); 
       ConPassword.setText(""); 
      } 
     } 
    }); 

    AlertDialog alertDialog = builder.create(); 
     alertDialog.show(); 

} 
} 

这里是我的MySingleton.java代码:

public class MySingleton { 

private static MySingleton mInstance; 
private RequestQueue requestQueue; 
private static Context mCtx; 
private MySingleton(Context context) 
{ 
    mCtx = context; 
    requestQueue = getRequestQueue(); 
} 


public RequestQueue getRequestQueue() 
{ 
    if(requestQueue==null) 
    { 
     requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); 
    } 
    return requestQueue; 
} 
public static synchronized MySingleton getInstance(Context context) 
{ 
    if(mInstance == null) 
    { 
     mInstance = new MySingleton(context); 
    } 
    return mInstance; 

} 

public <T> void addToRequestQue (Request<T> request) 
{ 
    requestQueue.add(request); 
} 
} 

这里是我的register.php代码:

<?php 

$user_name = $_POST["user_name"]; 
$name = $_POST["name"]; 
$surname = $_POST["surname"]; 
$email = $_POST["email"]; 
$birthday = $_POST["birthday"]; 
$address = $_POST["address"]; 
$postal_code = $_POST["postal_code"]; 
$city = $_POST["city"]; 
$country = $_POST["country"]; 
$mobile = $_POST["mobile"]; 
$password = $_POST["password"]; 

$sql = "select * from user_info where email like '".$email."';"; 

$result = mysqli_query($con,$sql); 
$response = array(); 

if(mysqli_num_rows($result)>0) 
{ 
$code = "reg_failed"; 
$message = "User already exists...."; 
array_push($response, array("code"=>$code, "message"=>$message)); 
echo json_encode($response); 
} 
else 
{ 
    $sql = "insert into user_info   values('".$user_name."','".$name."','".$surname."','".$email."','".$birthday."', 
            '".$address."','".$postal_code."','".$city."','".$country."','".$mobile."', '".$password."');"; 

$result = mysqli_query($con,$sql); 

$code = "reg_success"; 
$message = "Thank you for registering with us! Now you can login."; 
array_push($response, array("code"=>$code, "message"=>$message)); 
echo json_encode($response); 

} 

mysqli_close($con); 


?> 

:编辑:我的错误如下:

07-30 11:44:08.266 1824-2790/com.example.brian.loginnode E/Volley: [177]  NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference 
                   java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference 
                    at libcore.net.UriCodec.encode(UriCodec.java:132) 
                    at java.net.URLEncoder.encode(URLEncoder.java:57) 
                    at com.android.volley.Request.encodeParameters(Request.java:484) 
                    at com.android.volley.Request.getBody(Request.java:470) 
                    at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:253) 
                    at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:227) 
                    at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107) 
                    at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97) 
                    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114) 


                   [ 07-30 11:44:10.011 81: 81 D/   ] 
                   Socket deconnection 

                   [ 07-30 11:44:12.009 81: 81 D/   ] 
                   Socket deconnection 

                   [ 07-30 11:44:14.004 81: 81 D/   ] 
                   Socket deconnection 

                   [ 07-30 11:44:16.011 81: 81 D/   ] 
                   Socket deconnection 

                   [ 07-30 11:44:18.006 81: 81 D/   ] 
                   Socket deconnection 

                   [ 07-30 11:44:20.003 81: 81 D/   ] 
                   Socket deconnection 
+0

有一件事'电子邮件像''。$ email。''' - 使用'='而不是LIKE。你可能会得到不想要的/意想不到的结果。 –

+2

有没有错误,因为你永远不会检查他们。你简直假设什么都不可能出错,这是完全错误的态度。你很容易受到[sql注入攻击](http://bobby-tables.com) –

+0

@MarcB请阅读,我是PHP和SQL的新手。我正在关注YouTube上的教程。我来这里寻求帮助。你对你的同行编码先生有错误的态度。 – Brian

回答

0

基本上,你的onErrorResponse方法应该做一些事情来记录错误信息(例如,显示一个弹出窗口 - 类似于displayAlert方法)。此外,由于您的异常处理程序只是执行e.printStackTrace(),所以如果有任何异常抛出,它们将被打印到控制台 - 无论它在哪里。因此,您可能还需要包含一些警报对话框而不是e.printStackTrace() - 至少使用e.getMessage()显示警报。

+0

我现在增加了错误,是否响铃? – Brian