我正在android studio中进行注册活动,我希望它检查该数据库中是否已经存在具有相同名称的用户,如果有具有相同名称的用户,我希望它显示一条Toast消息,告知用户选择另一个用户名。
问题在于,每次我尝试注册用户时,都会显示一条消息,提示已经有一个使用该名称的用户(即使没有)。 这是我的代码:
register.php
<?PHP
include_once("connection.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo json_encode( "hola" );
$prueba="Luis";
if(isset($_POST['email']) && !empty(isset($_POST['email'])) && isset($_POST['password']) && !empty(isset($_POST['password']))){
include_once("connection.php");
$email = $_POST['email'];
$password = $_POST['password'];
$created_date = Date('Y-m-d H:m:s');
$status = 1;
$sql = "INSERT INTO table_user VALUES (NULL, '$email', '$password', '$created_date', $status)";
$CheckUsersSql="SELECT email FROM table_user";
$CheckUserQuery=$conn->query($CheckUsersSql);
$CheckUsers=$CheckUserQuery->fetch_assoc();
$Check=$CheckUsers['email'];
echo $Check;
while($CheckUsers=$CheckUserQuery->fetch_assoc()){
$CheckUsersSql="SELECT email FROM table_user";
$CheckUserQuery=$conn->query($CheckUsersSql);
$Check=$CheckUsers['email'];
if($prueba==$Check){
echo "ErrorDuplicate";
break;
}
else{
if ($conn->query($sql) === TRUE) {
$last_id = mysqli_insert_id($conn);
echo "$last_id";
break;
} else {
echo "ErrorInsert";
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
}
?>
RegisterActivity.java
package co.quindio.sena.navigationdrawerejemplo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.kosalgeek.android.md5simply.MD5;
import com.kosalgeek.genasync12.AsyncResponse;
import com.kosalgeek.genasync12.PostResponseAsyncTask;
import java.util.HashMap;
import co.quindio.sena.navigationdrawerejemplo.R;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
final String TAG = "RegisterActivity";
EditText etEmail, etPassword, etConfirmPassword;
Button btnRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etEmail = (EditText)findViewById(R.id.etEmail2);
etPassword = (EditText)findViewById(R.id.etPassword2);
etConfirmPassword = (EditText)findViewById(R.id.etConfirmPassword2);
btnRegister = (Button)findViewById(R.id.btnRegister2);
btnRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(!emptyValidate(etEmail, etPassword, etConfirmPassword)){
if(passwordValidate(etPassword, etConfirmPassword)){
String email = etEmail.getText().toString();
String password = MD5.encrypt(etPassword.getText().toString());
HashMap<String, String> postData = new HashMap<>();
postData.put("email", email);
postData.put("password", password);
PostResponseAsyncTask task1 = new PostResponseAsyncTask(this, postData, new AsyncResponse() {
@Override
public void processFinish(String s) {
Log.d(TAG, s);
if (s.contains("ErrorInsert")) {
Toast.makeText(RegisterActivity.this, "Fallo al registrarse", Toast.LENGTH_LONG).show();
} else {
if (s.contains("ErrorDuplicate")) {
Toast.makeText(RegisterActivity.this, "Ya existe ese nombre de usuario, favor elija otro.", Toast.LENGTH_LONG).show();
} else {
Intent in = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(in);
}
}
}
});
task1.execute("http://192.168.0.114/conecciones%20y%20demas/register.php");
}
else{ // not equals
Toast.makeText(getApplicationContext(), "Los campos de contraseña y confirmar contraseña deben ser iguales.", Toast.LENGTH_LONG).show();
}
} else{
Toast.makeText(getApplicationContext(), "Favor no dejar ningun espacio vacio.", Toast.LENGTH_LONG).show();
}
}
private boolean emptyValidate(EditText etEmail, EditText etPassword, EditText etConfirmPassword){
String email = etEmail.getText().toString();
String password = etPassword.getText().toString();
String confirm = etConfirmPassword.getText().toString();
return (email.isEmpty() && password.isEmpty() && confirm.isEmpty());
}
private boolean passwordValidate(EditText etPassword, EditText etConfirmPassword){
String password = etPassword.getText().toString();
String confirm = etConfirmPassword.getText().toString();
return (password.equals(confirm));
}
}
答案 0 :(得分:0)
在php文件中,您可以检查用户名是否存在或不购买 从table_user中选择,其中用户名= $ _POST ['用户名'] 并确定是否满足条件,然后输入该用户名(如果为假)