php - 使用PHP;我想检查 mysql 数据库以确保在注册之前没有相同电子邮件地址的记录

标签 php mysql mysqli

这是我的表格:

<section class="loginform tmr">
    <form name="login" action="welcome.php" method="post" accept-charset="utf-8">
        <label for="username">Username: </label><br />
            <?php if (isset($input_errors['username'])) { echo '<div class="error">' . $input_errors['username'] . '</div>'; } ?>
            <input type="username" name="username" placeholder="Handle" required><br />
            <input type="hidden" name="sign_up_date" value="<?php echo $_POST['sign_up_date'] ?>">
        <label for="usermail">Email: </label><br />
            <?php if (isset($input_errors['usermail'])) { echo '<div class="error">' . $input_errors['usermail'] . '</div>'; } ?>
            <input type="email" name="usermail" placeholder="yourname@email.com" required><br />
        <label for="password">Password: </label><br />
            <input type="password" name="password" placeholder="password" required><br />
            <input type="submit" value="Login">
    </form>
    </section>

这是我对插入内容的所有验证:

$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();
}

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);
    }
}

我想检查用户名和电子邮件,如果其中任何一个正在使用,则提醒新注册用户...

我认为我的服务器端验证内容变得如此困惑,也许我应该为此构建一个类...??

这不一定是我需要atm帮助的东西。我真的只是想使用最佳实践检查mysql是否正在使用电子邮件或用户名正在使用。

任何和所有的帮助将不胜感激。谢谢。

最佳答案

尝试以下或点击链接 php mysqli check if any result exist

<?php
    function user_exists($email) {
        $query = "SELECT 1 FROM " . USER_TABLE . " WHERE email = ?";
        $stmt = $this->_db->prepare($query);
        $stmt->execute(array($email));
        return (bool)$stmt->fetchColumn();
    }

    //do your stuff here same for username
    if(user_exits($register_email))

关于php - 使用PHP;我想检查 mysql 数据库以确保在注册之前没有相同电子邮件地址的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20833531/

相关文章:

php - 在php中编写sql代码并用php将变量写入文件

PHP mysqli 多查询+事务

php - 使用 Laravel 5.1 获取过去一小时内添加的行

php - 使用数组中的参数生成 URL

php - 如何在运行时(动态地)创建 PHP 静态类属性?

Javascript - PHP 在 JavaScript 上的 Substr() 替代品

php - 使用SQLi数据库从表单插入数据

php - 使用 css 和 jquery 围绕画廊中的事件图像边框

php - MySQL:如何使表中多行的数据唯一?

php - mysql/php 在重复键上插入/更新