Skip to content

Instantly share code, notes, and snippets.

@arif98741
Last active June 10, 2018 00:22
Show Gist options
  • Save arif98741/ad73dd48650ff1d42490f38645ff4ae8 to your computer and use it in GitHub Desktop.
Save arif98741/ad73dd48650ff1d42490f38645ff4ae8 to your computer and use it in GitHub Desktop.
Check wheather Email is Exist and Registration
<?php
/*
@ filter email
@ check email
@return json response
developer: Ariful Islam
website: phpdark.com
email: [email protected]
github.com/arif98741
expert in php,laravel,codeigniter,node,angular
*/
if (isset($_POST['checkmail'])) { //give a post request from application
/*
*@param1 = host, param2 = username,param3 = password, param4 = databasename
*/
$db = new mysqli("localhost","root","","database"); //
$response = array(); //this will give response to the application
if ($db) { //if connection true
$email = mysqli_real_escape_string($db,$_POST['email']);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { //execute if email is valid
$query = "select * from registration where email='$email'";
$stmt = $db->query($query) or die($db->error)."error at line number ".__LINE__;
if ($stmt) {
if ($stmt->num_rows > 0) { //check wheather email is exist
$response['error'] = "Email is already exist";
} else{
$insertQuery = "select into registration(name,email,address) values('jhon','[email protected]','new york city')";
$insertStmt = $db->query($insertQuery) or die($db->error)."error at line number ".__LINE__;
if ($insertStmt) { //if data insert successful
$response['message'] = "Successfully inserted to database";
} else { //if any error occurs
$response['error'] = "Failed to insert data";
}
}
} else { //failed query has error
$response['error'] = "Failed to execute query";
}
} else { //assign value to error index of response array
$response['error'] = "Email is not valid";
}
} else {
$response['error'] = "Failed to connect to database";
}
echo json_encode($response);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment