简体   繁体   English

PHP创建2行并且不检查输入

[英]PHP creating 2 rows and not checking input

What I'm trying to do is make a username (just username) get sent to a MySQL database, if it isn't already there. 我正在尝试做的是将用户名(仅用户名)发送到MySQL数据库(如果尚未存在)。

Basically, I'm getting the input, checking it against all other rows in my username column, and, if the input is not the same as any of them, then add the input to the database. 基本上,我得到了输入,并与我的username名列中的所有其他行进行了比较,并且,如果输入与任何其他行都不相同,则将输入添加到数据库中。 Here is my code: 这是我的代码:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}



if( isset( $_POST["submit"] ) ) {
    $sql="INSERT INTO users (username)
    VALUES('$_POST[username]')";


    $result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        if ($row["username"] == $_POST[username]) {
            die("Username is already in use!");
        }
    }
}


if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    die("Error, please consult an admin: " . $sql . "<br>" . $conn->error);
    }
$conn->close();

No error is reported, but it simply creates the data twice, and doesn't check the input. 没有错误报告,但是它只创建两次数据,并且不检查输入。 I can't see how. 我看不出来 This is how I've tried. 这就是我尝试过的方式。 It looks logical that is should work, but it's not. 看起来应该合乎逻辑,但事实并非如此。 Why? 为什么?

I'm using MySqli Object-Orientated 我正在使用MySqli面向对象

You are executing the $conn->query($sql) twice. 您正在执行$ conn-> query($ sql)两次。 The first one is in $result = $conn->query($sql); 第一个是在$result = $conn->query($sql); and the second if ($conn->query($sql) === TRUE) . 第二个if ($conn->query($sql) === TRUE) That is why you get 2 entries in the for one request. 这就是为什么您在一个请求中有2个条目的原因。

First check for the user you are prepering to insert and if it returns 0 statements then go with the second if you have wrote. 首先检查您准备插入的用户,如果它返回0语句,那么if您已经写过,则进行第二个检查。

Edit 2: 编辑2:

Try use PDO: 尝试使用PDO:

The code will look something like this: 该代码将如下所示:

if( isset( $_POST["submit"] ) ) {
    $stmt = $pdo->prepare("SELECT username FROM users WHERE username LIKE ?");
    $stmt->execute( array( $_POST["username"] ) );
    $_results = $stmt->get_result();

    if( count( $_results ) > 0 ) {
        die("Error, please consult an admin!");
    } else {
        $stmt = $pdo->prepare('INSERT INTO users (username) VALUES( ? )' );
        $stmt->bindParam( 1, $_POST['username'] );

        if( $stmt->execute() ) {
          echo 'Success';
        } else {
          echo 'Whoops, you have an error mate!';
        }
    }
}

Hope it helps 希望能帮助到你

Try this code... 试试这个代码...

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}



if( isset( $_POST["submit"] ) ) {
    $sql="INSERT IGNORE INTO users (username)
    VALUES('$_POST[username]')";


    $result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        if ($row["username"] == $_POST[username]) {
            die("Username is already in use!");
        }
    }
    echo "New record created successfully";
}else {
    die("Error, please consult an admin: " . $sql . "<br>" . $conn->error);
}
$conn->close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM