【问题标题】:PHP check if value exists in Database [duplicate]PHP检查数据库中是否存在值[重复]
【发布时间】:2017-11-24 21:48:00
【问题描述】:

我正在使用 MySQL,我想检查用户输入 $_POST['username'] 的值是否已存在于我的数据库中(在字段 username 中)。我试过这段代码:

$usernameExists = "SELECT * FROM users WHERE username = " . $_POST['username'];

if ($usernameExists) {
    echo "Exists"
} 

我把这段代码放在if (!empty...) 语句之后;

但什么也没发生。如果您需要我的完整代码,可以在此处获得,但我认为其余部分不会有帮助:

<?php

session_start();

if (isset($_SESSION['user_id'])) { // user is already logged in
    header("Location: index.php");
}

require('database.php');

$message = '';
$emailMessage = '';
$usernameMessage = '';
$passwordMessage = '';
$confirmMessage = '';

if (!empty($_POST['email']) && !empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['confirmPassword'])) { // user submitted form; enter user

    if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $emailMessage = 'Invalid email.';

    } elseif (strlen($_POST['username']) < 4 || strlen($_POST['username']) > 250) {
        $usernameMessage = 'Username has to be between 4 and 250 characters.';

    } elseif (!preg_match("/^[a-zA-z0-9]*$/", $_POST['username'])) {
        $usernameMessage = 'Username can only contain numbers and letters.';

    } elseif (strlen($_POST['password']) < 6 || strlen($_POST['password']) > 250) {
        $passwordMessage = 'Password has to be between 6 and 250 characters.';

    } elseif ($_POST['confirmPassword'] !== $_POST['password']) {
        $confirmMessage = 'Passwords don\'t match THONK';

    } else {
        $sql = "INSERT INTO users (email, username, password) VALUES (:email, :username, :password)";
        $stmt = $conn->prepare($sql);

        $stmt->bindParam(':email', $_POST['email']);
        $stmt->bindParam(':username', $_POST['username']);

        $password = password_hash($_POST['password'], PASSWORD_BCRYPT);
        $stmt->bindParam(':password', $password);

        if ($stmt->execute()) {
            $message = 'Successfully created new user: ' . $_POST['username'];
        } else {
            $message = 'There was an error lol';
        }
    }
}
?>

【问题讨论】:

  • username = " . $_POST['username'] 是无效的 SQL 并让您面临 SQL 注入。除非您的用户名都是整数。您当前使用我的名字进行的查询是SELECT * FROM users WHERE username = chris85。像在其他地方一样参数化该查询。
  • 您在 INSERT 语句中使用了准备好的语句。对您的 SELECT 语句执行相同的操作,您应该已经设置好了。
  • 您是否也曾将$usernameExists 发送到数据库?如果不是那是第一个问题。
  • @MagnusEriksson 目前我并不担心准备好的陈述。我只是想让它出现,但是我在 OP 中的声明不起作用。如果该用户名已经存在,一旦我得到回显,我会担心使其安全;)
  • @MuhammadUsman OP 似乎使用的是 PDO,而不是 mysqli 的程序版本。即使 OP did 使用它,您的示例也是无效的。该函数有两个参数,第一个是数据库链接。

标签: php mysql


【解决方案1】:

使用准备好的语句查询数据库。像这样:

 $usernameExists = 0;
 $sql = 'SELECT username FROM users WHERE username = :username';
 $stmt = $conn->prepare($sql);
 $stmt->bindValue(':username',$_POST['username']);
 $stmt->execute();

 if($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // row(s) returned
    $usernameExists = 1;
 } else {
    // no row returned
    $usernameExists = 0;
 }
 $stmt->closeCursor();

那么你可以这样做:

if ($usernameExists) {
   echo "Exists"
} 

【讨论】:

    猜你喜欢
    • 2016-03-01
    • 1970-01-01
    • 2021-10-10
    • 2020-07-31
    • 1970-01-01
    • 2018-03-05
    • 2013-05-11
    • 2011-12-22
    • 2012-01-26
    相关资源
    最近更新 更多