-2

我有一个名为reg.php的文件,它为登录系统扩展了我的registration.php

注册与 mysqli 数据库连接,我在 php 中手动增加了用户 ID,而不是在 mysqli 中自动增加,因为我无法完成这项工作。

当我点击注册按钮时,我的代码在数据库中创建了两个条目,我不知道为什么。

<?php

if(isset($_POST["register_user"])){
header('location: login.php');
}



if(isset($_POST["username"])){                                                  
    $username = mysqli_real_escape_string($db, $_POST['username']); 
}

if(isset($_POST["email"])){
    $email = mysqli_real_escape_string($db, $_POST['email']);
}

if(isset($_POST["password"])){
    $password = mysqli_real_escape_string($db, $_POST['password']);
}

if(isset($_POST["passwordconfirm"])){
    $passwordconfirm = mysqli_real_escape_string($db, $_POST['passwordconfirm']); 
}


$idcount = mysqli_num_rows(mysqli_query($db, "SELECT id FROM user"));       


$regquery = "INSERT INTO user (id, name, email, password) VALUES ('$idcount', '$username', '$email', '$password')";
mysqli_query($db, $regquery);

?>

我想要的只是一个条目。

4

1 回答 1

0

在您的问题中,代码中没有任何内容可以在您的数据库中插入两行。我猜你调用这个脚本两次,可能是因为你的表单有两种提交相同数据的方式:

  1. 正常方式,带有表单动作属性。
  2. 通过一个事件,比如onSubmit

如果使用后者,您必须停止前者。查看 2 链接中的示例。它还告诉您如何阻止表单提交:

  // For this example, don't actually submit the form
  event.preventDefault();

因此,如果onSubmit有效,它将停止正常的表单提交,但如果它不起作用,则以正常方式提交表单。

于 2019-06-08T09:00:49.747 回答