我的代码正在运行,除了它将双重数据插入数据库,例如,如果我输入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.';
}
}
?>
答案 0 :(得分:4)
你运行两次
$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.';
}
}