繁体   English   中英

PDO插入双数据

[英]PDO inserting double data

我的代码正在工作,除了它将双重数据插入数据库,例如,如果我输入john作为用户名,123输入密码,它会插入两次! 我怎么能摆脱它?

<form method="POST">
    <input type="text" name="username" placeholder="username"><br />
    <input type="password" name="password" placeholder="password"><br />
    <input type="submit">
</form>

<?php
    if (isset($_POST['username'], $_POST['password'])) {
        require 'core/connect.php';

        $query = dbConnect()->prepare("INSERT INTO users (username, password) VALUES(?,?)");

        $query->bindParam(1, $_POST['username']);
        $query->bindParam(2, $_POST['password']);
        $query->execute();

        if ($query->execute()) {
        echo 'Thank you! you may now log in <a href=\'index.php\'>here</a>';
        } else {
        echo 'Please go back, something was wrong.';
        }
    }
?>

你跑两次

$query->execute();

if ($query->execute()) {

省略第一行

所以你留下来:

if (isset($_POST['username'], $_POST['password'])) {
    require 'core/connect.php';

    $query = dbConnect()->prepare("INSERT INTO users (username, password) VALUES(?,?)");

    $query->bindParam(1, $_POST['username']);
    $query->bindParam(2, $_POST['password']);

    if ($query->execute()) {
        echo 'Thank you! you may now log in <a href=\'index.php\'>here</a>';
    } else {
        echo 'Please go back, something was wrong.';
    }
}

暂无
暂无

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

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