错误检查php Android Studio中的重复项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误检查php Android Studio中的重复项相关的知识,希望对你有一定的参考价值。

我正在我的android工作室进行注册活动,我希望它检查在该数据库中是否已经有同名用户,如果有同名用户我希望它显示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));
    }
}
答案

在php文件中,您可以检查用户名是否存在或者没有购买选择table_user,其中username = $ _POST ['username']并且如果条件为结果如果ture然后此用户名如果​​为false则可以注册

以上是关于错误检查php Android Studio中的重复项的主要内容,如果未能解决你的问题,请参考以下文章

错误记录Android Studio 编译时 lint 检查报错 ( Could not resolve junit:junit:4.+. )

Android Studio中的版本同步失败

如何在 Android Studio 的 README.md 文件中禁用代码检查错误

我的Android进阶之旅解决Android Studio的Translate插件报错 TKK:更新TTK失败,请检查网络连接问题

我的Android进阶之旅解决Android Studio的Translate插件报错 TKK:更新TTK失败,请检查网络连接问题

我的Android进阶之旅解决Android Studio的Translate插件报错 TKK:更新TTK失败,请检查网络连接问题