Wednesday, 28 August 2013

User class not returning duplicate emails as false in database

User class not returning duplicate emails as false in database

I've made a user class which validates the data passed through the form
and then subsequently updates the database table users. I want to add
extra functionality such as checking if the username and email exists in
the table, I've acquired some help from members of stackoverflow in order
to start building this, however the below code is still allowing the
duplicate emails to be inserted into the database, I don't get any sql
error messages just a success message as follows "1 Row inserted."
Obviously I expect the error message "email exists"
Any help would be helpful.
public function insert() {
$result = $this->mysqli->prepare("SELECT COUNT(*) FROM users WHERE email=?");
$result->bind_param("s", $_POST['email']);
$result->execute();
$result->bind_result($email_count);
$result->close();
if ($email_count > 0) {
echo "email exisits!";
} else {
//escape the POST data for added protection
$username = isset($_POST['username']) ?
$this->mysqli->real_escape_string($_POST['username']) : '';
$cryptedPassword = crypt($_POST['password']);
$password = $this->mysqli->real_escape_string($cryptedPassword);
$name = isset($_POST['name']) ?
$this->mysqli->real_escape_string($_POST['name']) : '';
$email = isset($_POST['email']) ?
$this->mysqli->real_escape_string($_POST['email']) : '';
$stmt = $this->mysqli->prepare("INSERT INTO users (username, password,
name, email) VALUES (?, ?, ?, ?)");
//var_dump($this->mysqli->error);
$stmt->bind_param('ssss', $username, $password, $name, $email); //
bind strings to the paramater
/* execute prepared statement */
$stmt->execute();
printf("%d Row inserted.\n", $stmt->affected_rows);
/* close statement and connection */
$stmt->close();
} // end email_count and insert to table
} // end function
i have used this ajax code attached below and it worked for me.... try if
it is helpful....
ajax code-----------------
<script type="text/javascript">
$(document).ready(function() {
// check username availability on focus lost
$('#username').focusout(function() {
if ($('#username').val()!='') {
var username=$('#username').val();
$.ajax({
url:
"/gtlqa/checkUserNameAvailability/checkAvailability.html?username="+username,
method:'GET',
success:function(data){
if(data!="available")
{
$('input:submit').attr("disabled", true);
$("#usernameErrorMessage").html("username
already exists");
}
else
{
$("#usernameErrorMessage").html("available");
$('input:submit').attr("disabled", false);
}
}
});
}
});
});
function checkAvailability() {
$.getJSON("/checkUserNameAvailability", {
username : $('#username').val()
}, function(available) {
if (available) {
fieldValidated("username", {valid : true});
$('input:submit').attr("disabled", false);
} else {
$('input:submit').attr("disabled", true);
fieldValidated("username", {valid : false, message :
$('#username').val() + " is not available"});
}
});
}
</script>
controller----------------
@RequestMapping(value="checkAvailability",method = RequestMethod.GET)
public @ResponseBody String getAvailability(@RequestParam("username")
String username) {
System.out.println("username is"+username);
boolean status=userService.available(username);
if(status)
{
return "available";
}
else
return "username already exists";
}

No comments:

Post a Comment