PHP检查数据库的用户ID

时间:2014-07-31 23:57:40

标签: php mysql ajax database

我使用jquery ajax将数据发送到php doc。对用户输入的第一次检查( $ check_user_exists )是查看是否有相同 $ email 下的帐户。我曾尝试在if语句之前编写一个函数但是也没有用。

我收到以下错误:

  

解析错误:语法错误,意外'返回'第8行的C:\ xampp \ htdocs \ workflow \ ajax \ register.php中的(T_RETURN)

提前致谢!这是代码:

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.';
    } 

1 个答案:

答案 0 :(得分:1)

这样做:

$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.';
}