0

I'm sending data to a php doc with jquery ajax. The first check ($check_user_exists) on the user's inputs is to see if there's an account under the same $email. I've tried writing a function before the if statement but that didn't work either.

I'm getting the following error:

Parse error: syntax error, unexpected 'return' (T_RETURN) in C:\xampp\htdocs\workflow\ajax\register.php on line 8

Thanks ahead of time! Here's the code:

if (isset($_POST['email']) && isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['role']) && isset($_POST['pw'])){
    $email = strtolower($_POST['email']);
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $pw = crypt($_POST['pw'], md5($email));
    $role = $_POST['role'];
    $check_user_exists = return (mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE user_id = '$email.'"), 0)==1) ? true : false;
    if($check_user_exists === true){
        echo 'Our records show an account already exists under this email.';
    } 
9
  • 1
    your userid is the email? Commented Jul 31, 2014 at 23:58
  • 1
    Using mysql_num_rows() would cut down your code by no less than 30%. Commented Jul 31, 2014 at 23:59
  • "Didn't work" is not good enough. Explain what the problem is. Commented Aug 1, 2014 at 0:00
  • 1
    mysql_result() returns the contents of the user_id field. Since your user IDs are apparently their email, not an identifier number, it will never be == 1. Commented Aug 1, 2014 at 0:00
  • 1
    Fred ii, thanks for catching that error! Commented Aug 1, 2014 at 0:27

1 Answer 1

1

Do it like this:

$email = mysql_real_escape_string(strtolower($_POST['email']));
$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$pw = crypt($_POST['pw'], md5($email));
$role = mysql_real_escape_string($_POST['role']);
$result = mysql_query("SELECT COUNT(*) AS count FROM `users` WHERE user_id = '$email'") or die(mysql_error());
$row = mysql_fetch_assoc($result);
$check_user_exists = $row['count'];
if ($check_user_exists > 0){
    echo 'Our records show an account already exists under this email.';
} else {
    mysql_query("INSERT INTO users (user_id, firstname, lastname, pw, role)
                 VALUES ('$email', '$firstname', '$lastname', '$pw', '$role')") or die(mysql_error());
    echo 'User added successfully.';
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hey Barmar, looks like it works in the sense that it doesn't add any new entries but it doesn't echo the error message. I forgot to mention that my database is called 'workflow' and it's hosted on localhost. so I added a select db statement, but it still doesn't work
You had an extra . after $email in the query. I've added the code to add the new entry. One of the two messages should be echoed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.