我需要检查我的数据库以查看用户名或电子邮件是否已被使用

时间:2014-01-03 00:24:48

标签: php mysql mysqli mysqlnd

到目前为止,我已经开始了一两个线程,但没有解决任何问题。我无法使用mysqlnd,因为我正在使用godaddy的共享主机帐户。

我需要做的就是检查我的电子邮件地址和/或用户名是否正在使用中;如果他们在使用投掷和错误,如果没有..一切都很好。

这是我的代码:

$input_errors = array();

if (!empty($_POST['username'])) {
    $user = $_POST['username'];
} else {
    $input_errors['username'] = "Must fill out username";
}

$email = filter_input(INPUT_POST, 'usermail', FILTER_VALIDATE_EMAIL);
if (false === $email) {
    $input_errors['usermail'] = "Not a valid email address";
}

if(count($input_errors) > 0) {
    print_r($input_errors); die();
}

$sql = "SELECT COUNT(*) as amount FROM people WHERE username = ? 
       OR email = ?";
if ($stmt = $mysqli->prepare($sql)) {

    $stmt->bind_param("ss", $user, $email);
    $stmt->execute();
    $results = $stmt->get_result();
    $data = mysqli_fetch_assoc($results);

    if ($data['amount'] > 0)
    {
        print "User already exists";
    }
}

else {
$stmt = $mysqli->stmt_init();
if (!$stmt) {
    echo "Init failed";
} else {
    $cmd = "INSERT INTO people (username, email, sign_up_date) VALUES (?, ?, NOW() )";
    if ($stmt->prepare($cmd)) {
        $stmt->bind_param('ss', $user, $email );
        $stmt->execute();

        echo $stmt->affected_rows . " row(s) inserted";

        $stmt->close();

    } else {
        echo "Prepare failed";
    }
    mysqli_close($mysqli);
    }
}

bind_result()不起作用。

1 个答案:

答案 0 :(得分:0)

将sql语句更改为以下内容:

$sql = "SELECT COUNT(*) as amount FROM people WHERE username = '".mysqli_real_escape_string($_POST['username'])."' OR email = '".mysqli_real_escape_string($email)."'";