我一直困在一个困难的地方。使用齐射,我尝试在000webhost上的数据库中创建用户。因此,我使用了Register.php
<?php
$connect = mysqli_connect("localhost", "id6777294_l2w", "123l2w456", "id6777294_l2w");
if(isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["password"]) && isset($_POST["grade"])){
$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];
$class = $_POST["grade"];
}
$statement = mysqli_prepare($connect, "INSERT INTO user(name, email, password, grade) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "sssi", $name, $email, $password, $grade);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
以及以下两个android类
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText nameID = (EditText) findViewById(R.id.nameID);
final EditText emailID = (EditText) findViewById(R.id.emailID);
final EditText passwordID = (EditText) findViewById(R.id.passwordID);
final EditText gradeID = (EditText) findViewById(R.id.gradeID);
final Button registerbtn = (Button) findViewById(R.id.registerbtn);
registerbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//we need to get the data given by the user
final String name = nameID.getText().toString();
final String email = emailID.getText().toString();
final String password = passwordID.getText().toString();
final int grade = Integer.parseInt(gradeID.getText().toString());
//now we need a String Listener for the constructor
Response.Listener<String> responseListener = new Response.Listener<String>() {
/**
* everything in here happens when the response listener has been executed,
* given the response (which comes as a JSON String) from the Register.php.
* If the response is not in the form of a JSON format, then JSONException
* will catch the error
*/
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
//if successful then go back to LoginActivity, if not make an alert
if (success){
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
RegisterActivity.this.startActivity(intent);
}else {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("Registrierung fehlgeschlagen").setNegativeButton("Wiederhole",null).create().show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
//this actual creates the request we need for registration
RegisterRequest registerRequest = new RegisterRequest(name, email, password, grade, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}
}
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText nameID = (EditText) findViewById(R.id.nameID);
final EditText emailID = (EditText) findViewById(R.id.emailID);
final EditText passwordID = (EditText) findViewById(R.id.passwordID);
final EditText gradeID = (EditText) findViewById(R.id.gradeID);
final Button registerbtn = (Button) findViewById(R.id.registerbtn);
registerbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//we need to get the data given by the user
final String name = nameID.getText().toString();
final String email = emailID.getText().toString();
final String password = passwordID.getText().toString();
final int grade = Integer.parseInt(gradeID.getText().toString());
//now we need a String Listener for the constructor
Response.Listener<String> responseListener = new Response.Listener<String>() {
/**
* everything in here happens when the response listener has been executed,
* given the response (which comes as a JSON String) from the Register.php.
* If the response is not in the form of a JSON format, then JSONException
* will catch the error
*/
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
//if successful then go back to LoginActivity, if not make an alert
if (success){
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
RegisterActivity.this.startActivity(intent);
}else {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("Registrierung fehlgeschlagen").setNegativeButton("Wiederhole",null).create().show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
//this actual creates the request we need for registration
RegisterRequest registerRequest = new RegisterRequest(name, email, password, grade, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}
}
我没有收到来自logcat等的错误或提示。单击“注册”按钮时,我返回到LoginActivity,这似乎很完美。但是,当使用phpmyadmin检查数据库时,“用户”数据中没有新条目。
请帮助,我做了所有建议的改进并清除了常见的错误,但是它不会存储数据...
答案 0 :(得分:0)
在您的bind参数语句中,您要传递参数$ grade,但是我看不到$ grade变量已初始化,您是要传递$ class吗?可能是开始检查这一点的方法。