I have a mobile app that allow technicians to register, I want to prevent duplicated email address and mobile number, I wrote this code in my php file
<?php
require "config.php";
$name = $_POST['name'];
$password = $_POST['userpass'];
$emailadd = $_POST['emailadd'];
$phone = $_POST['phone'];
$category = $_POST['category'];
$token = $_POST['token'];
$username = stripslashes($username);
$password = stripslashes($password);
$sql_get_email= "SELECT * FROM technician where emailadd ='$emailadd';";
$result1 = mysqli_query($db, $sql_get_email);
$row = mysqli_fetch_array($result1,MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count >0) {
echo "Email already exists in our database";
die();
}
else {
$mysql_get_phone = "SELECT * FROM technician where mobile ='$phone';";
$result1 = mysqli_query($db, $sql_get_email);
$row = mysqli_fetch_array($result1,MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count >0) {
echo "Pnone number already exists in our database";
die();
}
else {
$sql = "INSERT INTO technician (name, emailadd, password, mobile, category, fcm_key)
VALUES ('$name', '$emailadd', '$password','$phone', '$category','$token')";
if ($db->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $db->error;
}
}}
$db->close();
?>
and I have this excerpt from my BackgroundTask.java to check for the result of the registration
@Override
protected void onPostExecute(String result) {
if (result.equals("Email already exists in our database")) {
final Dialog dialog = new Dialog(ctx);
dialog.setContentView(R.layout.dialog_second);
dialog.setTitle("ERROR");
dialog.setCancelable(true);
//set up text
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText(result);
//set up button
Button button = (Button) dialog.findViewById(R.id.btnOk);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
} else if (result.equals("Pnone number already exists in our database")) {
final Dialog dialog = new Dialog(ctx);
dialog.setContentView(R.layout.dialog_second);
dialog.setTitle("ERROR");
dialog.setCancelable(true);
//set up text
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText(result);
//set up button
Button button = (Button) dialog.findViewById(R.id.btnOk);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}else
if (result.equals("Registration Success...")) {
Toast.makeText(ctx, "Registration Successful......Please login", Toast.LENGTH_LONG).show();
Intent intent = new Intent(ctx, Login.class);
ctx.startActivity(intent);
I found out that the registration is always successful even with duplicated entry.
config.php
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'proartisan_dbadmin');
define('DB_PASSWORD', 'YYYYYYYYY');
define('DB_DATABASE', 'XXXXXXXXX');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>