我是android和MySQL的新手,我面临着在MySQL数据库中存储表单数据的问题。 我正在使用000webhost来存储我的php文件。 通过使用volley,我解析了数据。 没有错误,但没有存储数据。 我在下面附上了代码:
<?php
$conn = mysqli_connect("localhost", "id14252713_root", "Pranavkumar@9828", "id14252713_rhythmcaster");
$uname = $_POST['name'];
$uemail = $_POST['email'];
$upass = $_POST['password'];
$mobile = $_POST['phonenumber'];
$query = "INSERT INTO users(name,email,password,phonenumber) VALUES ('$uname','$uemail','$upass','$mobile')";
$result = mysqli_query($conn, $query);
if ($result) {
echo "Successfully registered.";
}
else{
echo "Failed to register.";
}
mysqli_close($conn);
?>
我的Java密码是
public class NewAccount extends AppCompatActivity {
EditText username, useremail, upass, mobile;
TextInputLayout passwordLayout, nameLayout, emailLayout, phoneLayout;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_account);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
username = (EditText) findViewById(R.id.username);
useremail = (EditText) findViewById(R.id.email);
upass = (EditText) findViewById(R.id.password);
mobile = (EditText) findViewById(R.id.phone);
submit = (Button) findViewById(R.id.signup);
passwordLayout = (TextInputLayout) findViewById(R.id.password_layout);
nameLayout = (TextInputLayout) findViewById(R.id.name_layout);
emailLayout = (TextInputLayout) findViewById(R.id.email_layout);
phoneLayout = (TextInputLayout) findViewById(R.id.phone_layout);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerUser();
}
});
}
private void registerUser() {
final String name = username.getText().toString();
final String email = useremail.getText().toString();
final String password = upass.getText().toString();
final String phonenumber = mobile.getText().toString();
if (name.isEmpty() || email.isEmpty() || password.isEmpty() || phonenumber.isEmpty()){
Toast.makeText(this, "Enter all the details", Toast.LENGTH_SHORT).show();
passwordLayout.setBoxStrokeColor(Color.RED);
phoneLayout.setBoxStrokeColor(Color.RED);
nameLayout.setBoxStrokeColor(Color.RED);
emailLayout.setBoxStrokeColor(Color.RED);
}
else if (password.length() < 8){
upass.setError("Password should be atleast 8 characters");
upass.setFocusable(true);
passwordLayout.setBoxStrokeColor(Color.RED);
}
else if (phonenumber.length() != 10){
phoneLayout.setError("Mobile number should be 10 numbers");
phoneLayout.setFocusable(true);
phoneLayout.setBoxStrokeColor(Color.RED);
}
else if (name.matches("(.*[0-9].*)") || name.matches("(.*[,~,!,@,#,$,%,^,&,*,(,),-,_,=,+,[,{,],},|,;,:,<,>,/,?].*$)")){
username.setError("Name field is invalid.");
username.setFocusable(true);
nameLayout.setBoxStrokeColor(Color.RED);
}
else if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
useremail.setError("Check Email ID");
useremail.setFocusable(true);
emailLayout.setBoxStrokeColor(Color.RED);
}
else {
String url = "https://rhythmcaster.000webhostapp.com/register.php";
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response.equals("Successfully registered")){
Toast.makeText(NewAccount.this, "Registered Successfully", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(NewAccount.this, response, Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(NewAccount.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}
){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("email", email);
map.put("password", password);
map.put("phonenumber", phonenumber);
return map;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(NewAccount.this);
requestQueue.add(request);
}
}
首先,您必须检查您的API是否工作,以检查使用chrome扩展,如talent或postmen如果数据成功插入使用API,那么问题出在您的代码中
我提供了一些适合您代码
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(NewAccount.this, response, Toast.LENGTH_SHORT).show();
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(NewAccount.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> parms = new HashMap<String, String>();
parms.put("name", name);
parms.put("email", email);
parms.put("password", password);
parms.put("phonenumber", phonenumber);
return parms;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);