Php MySQL插入在一次提交时发生两次

Php MySQL插入在一次提交时发生两次,php,html,mysql,Php,Html,Mysql,我有一个PHP文件,它处理用户使用mysql注册的输入。。。我有一个问题,它使用户输入两次。。。所以,这只是注册表单中的一个输入。下面是我报名表的一半(最有用的部分) if($\u服务器[“请求\u方法”]=“发布”){ 需要(“db settings.php”); //保安 if(空($_POST['name'])){ echo“抱歉,全名输入为空,如果愿意,请重试。”; 模具(); }否则{ $fullname=$_POST['name']; } 如果(空($_POST['email']))

我有一个PHP文件,它处理用户使用mysql注册的输入。。。我有一个问题,它使用户输入两次。。。所以,这只是注册表单中的一个输入。下面是我报名表的一半(最有用的部分)

if($\u服务器[“请求\u方法”]=“发布”){
需要(“db settings.php”);
//保安
if(空($_POST['name'])){
echo“抱歉,全名输入为空,如果愿意,请重试。”;
模具();
}否则{
$fullname=$_POST['name'];
}
如果(空($_POST['email'])){
echo“很抱歉,电子邮件输入不正确,如果您愿意,请重试。”;
模具();
}否则{
$email=$_POST['email'];
}
if(空($_POST['password'])){
echo“对不起,密码为空,如果您愿意,请重试。”;
模具();
}否则{
$password=$_POST['password'];
//如果密码变量设置成功,现在就对其进行加密!
$password=password\u hash($password,password\u DEFAULT)。“\n”;
}
//记录用户IP和存储在变量中
$ip=$\服务器[“远程地址”];
//创建连接
$conn=newmysqli($servername,$username,$db\u password,$dbname);
//检查连接
如果($conn->connect\u错误){
die(“连接失败:”.$conn->connect\U错误);
}
$sql=“插入到`表ex`(全名、电子邮件、密码、ip)值('$fullname','$email','$password','$ip')”;
$stmt=$conn->prepare($sql);
//$stmt->bind_param('sss',$fullname,$email,$password,$ip);
$stmt->execute();
if($conn->query($sql)==TRUE){
echo“新用户创建成功,请等待激活…”;
}否则{
echo“Error:”.$sql.“
”$conn->Error; } $conn->close();
所以,在这里,我还将在下面的html代码中给出整个表单部分

  <form action="signup.php" method="post">
    <h1>Sign up</h1><br/>

    <span class="input"></span>
  <input type="text" name="name" placeholder="Full name" title="Format: Xx[space]Xx (e.g. John Doe)" autofocus autocomplete="off" required pattern="^\w+\s\w+$" />
    <span class="input"></span>
    <input type="email" name="email" placeholder="Email address" required />
    <span id="passwordMeter"></span>
    <input type="password" name="password" id="password" placeholder="Password" title="Password min 10 characters. At least one UPPERCASE and one lowercase letter" required pattern="(?=^.{10,}$)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$"/>

    <button type="submit" value="Sign Up" title="Submit form" class="icon-arrow-right"><span>Sign up</span></button>
  </form>

注册
注册

所以,代码中一定有什么东西让它输入两次…另外,我如何重置id号?因为每次我创建一个新用户时,这种情况都会发生(每次都会发生),然后我只删除这些用户,并且这些用户仍然算起来,就好像它们仍然存在一样。

您同时使用了
execute()
query()
,因此执行两次

首先,它在
$stmt->execute();
插入一行,然后在
$conn->query($sql)
插入另一行

注意:


更好的做法是坚持使用预先准备好的语句,并使用
execute()
来提高安全性,而不是使用
$conn->query($sql)
。有关差异的更多信息,请参见。

您同时使用了
execute()
query()
,因此执行了两次

首先,它在
$stmt->execute();
插入一行,然后在
$conn->query($sql)
插入另一行

注意:


更好的做法是坚持使用预先准备好的语句,并使用
execute()
来提高安全性,而不是使用
$conn->query($sql)
。有关差异的更多信息,请访问。

这是因为这一行。您不需要放置if-else语句

  if ($conn->query($sql) === TRUE) {
          echo "New user was created successfully, please wait for activation...";
      }
就这么做吧-

    $sql = "INSERT INTO `table-ex` (fullname, email, password, ip) VALUES ('$fullname', '$email', '$password', '$ip')";
  $stmt = $conn->prepare($sql);
  //$stmt->bind_param('sss', $fullname, $email, $password, $ip);
//Set the variables here for $fullname, $email, $password and $ip
 if($stmt->execute())

 {
      echo "New user was created successfully, please wait for activation...";
} 
  else { echo "There was a problem";}

  $stmt->close();
  $conn->close();
更新

对于id部分,我假设您使用的是
自动增量
,但我建议您手动插入它们,而不是依赖它。我建议您使用唯一的密钥派生函数并对它们进行编码(如果您希望它们是纯文本并将其用作id)


如果要跟踪其中有多少个条目,您可以始终使用
mysqli_num_rows()

计算行数,这是因为这一行。不需要使用If-else语句

  if ($conn->query($sql) === TRUE) {
          echo "New user was created successfully, please wait for activation...";
      }
就这么做吧-

    $sql = "INSERT INTO `table-ex` (fullname, email, password, ip) VALUES ('$fullname', '$email', '$password', '$ip')";
  $stmt = $conn->prepare($sql);
  //$stmt->bind_param('sss', $fullname, $email, $password, $ip);
//Set the variables here for $fullname, $email, $password and $ip
 if($stmt->execute())

 {
      echo "New user was created successfully, please wait for activation...";
} 
  else { echo "There was a problem";}

  $stmt->close();
  $conn->close();
更新

对于id部分,我假设您使用的是
自动增量
,但我建议您手动插入它们,而不是依赖它。我建议您使用唯一的密钥派生函数并对它们进行编码(如果您希望它们是纯文本并将其用作id)

如果要跟踪其中有多少个条目,可以始终使用
mysqli_num_rows()

If($\u SERVER[“REQUEST\u METHOD”]=“POST”)来计算行数{
需要(“db settings.php”);
//保安
if(空($_POST['name'])){
echo“抱歉,全名输入为空,如果愿意,请重试。”;
模具();
}否则{
$fullname=$_POST['name'];
}
如果(空($_POST['email'])){
echo“很抱歉,电子邮件输入不正确,如果您愿意,请重试。”;
模具();
}否则{
$email=$_POST['email'];
}
if(空($_POST['password'])){
echo“对不起,密码为空,如果您愿意,请重试。”;
模具();
}否则{
$password=$_POST['password'];
//如果密码变量设置成功,现在就对其进行加密!
$password=password\u hash($password,password\u DEFAULT)。“\n”;
}
//记录用户IP和存储在变量中
$ip=$\服务器[“远程地址”];
//创建连接
$conn=newmysqli($servername,$username,$db\u password,$dbname);
//检查连接
如果($conn->connect\u错误){
die(“连接失败:”.$conn->connect\U错误);
}
$sql=“插入到`表ex`(全名、电子邮件、密码、ip)值('$fullname','$email','$password','$ip')”;
$stmt=$conn->prepare($sql);
//$stmt->bind_param('sss',$fullname,$email,$password,$ip);
如果($stmt->execute()){
echo“新用户创建成功,请等待激活…”;
}否则{
echo“Error:”.$sql.“
”$conn->Error; } $stmt->close(); $conn->close();
if($\u服务器[“请求方法”]=“发布”){
需要(“db settings.php”);
//保安
if(空($_POST['name'])){
echo“抱歉,全名输入为空,如果愿意,请重试。”;
模具();
}否则{
$fullname=$_
    $sql = "INSERT INTO `table-ex` (fullname, email, password, ip) VALUES ('$fullname', '$email', '$password', '$ip')";
  $stmt = $conn->prepare($sql);
  //$stmt->bind_param('sss', $fullname, $email, $password, $ip);
//Set the variables here for $fullname, $email, $password and $ip
 if($stmt->execute())

 {
      echo "New user was created successfully, please wait for activation...";
} 
  else { echo "There was a problem";}

  $stmt->close();
  $conn->close();
 if ($_SERVER["REQUEST_METHOD"] == "POST") {

    require("db-settings.php");

    // Security

    if (empty($_POST['name'])) {
      echo "Sorry, fullname input was empty, please retry if you like.";
      die();
    } else {
      $fullname = $_POST['name'];
    }
    if (empty($_POST['email'])) {
      echo "Sorry, email input was emty, please retry if you like.";
      die();
    } else { 
      $email = $_POST['email'];
    }
    if (empty($_POST['password'])) {
      echo "Sorry, password was empty, please retry if you like.";
      die();
    } else {
      $password = $_POST['password'];

      // If password variable is success to set, let's encrypt it now!
      $password = password_hash($password, PASSWORD_DEFAULT)."\n";
    }

  // Log users IP and store in variable
    $ip = $_SERVER["REMOTE_ADDR"];

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

  $sql = "INSERT INTO `table-ex` (fullname, email, password, ip) VALUES ('$fullname', '$email', '$password', '$ip')";
  $stmt = $conn->prepare($sql);
  //$stmt->bind_param('sss', $fullname, $email, $password, $ip);


  if ($stmt->execute()) {
      echo "New user was created successfully, please wait for activation...";
  } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
  }
  $stmt->close();
  $conn->close();