停止PHP形式的重复条目

时间:2018-07-01 19:47:01

标签: php sql phpmyadmin

努力实现一种方法来停止此代码中的重复条目,我尝试了一些使用if和else语句的方法,但似乎无法正常工作。谁能提供解决方案?

<?php
require('db.php');
// If form submitted, insert values into the database.


if (isset($_REQUEST['username'])){
        // removes backslashes
    $username = stripslashes($_REQUEST['username']);
        //escapes special characters in a string
    $username = mysqli_real_escape_string($con,$username); 
    $email = stripslashes($_REQUEST['email']);
    $email = mysqli_real_escape_string($con,$email);
    $password = stripslashes($_REQUEST['password']);
    $password = mysqli_real_escape_string($con,$password);
    $signupdate = date("Y-m-d H:i:s");
        $query = "INSERT into `users` (username, password, email, signupdate)
VALUES ('$username', '".md5($password)."', '$email', '$signupdate')";
        $result = mysqli_query($con,$query);
        if($result){
            echo "<div class='form'>
<h3>You are registered successfully.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";
        }
    }else{
?>
<div class="form">
<form name="registration" action="" method="post">
<input type="text" name="username" placeholder="Username" required />
<input type="email" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<input type="submit" name="submit" value="Register" />
</form>
</div>
<?php } ?>      

2 个答案:

答案 0 :(得分:3)

  1. 替换
echo "<div class='form'>
<h3>You are registered successfully.</h3>
<br/>Click here to <a href='login.php'>Login</a></div>";

使用

header("location:thankyou.php");

并将文本添加到新页面。

  1. 点击“提交”按钮后将其禁用

更改

<input type="submit" name="submit" value="Register" />

使用

  

<input type="submit" name="submit" value="Register" onClick="this.disabled=true; this.value='Processing…'";>

3。将数据库中的电子邮件字段更改为唯一

ALTER TABLE users ADD UNIQUE (email);
  1. 在$ result检查中添加其他内容:
if($result){
     header("location:thankyou.php");
}
else{
     echo "Registration failed, Make sure to use an email address that was not used before";
}

答案 1 :(得分:0)

<?php 
If(isset($_POST[‘submit]))
{
   $username = stripslashes($_REQUEST['username']);
    $username = mysqli_real_escape_string($con,$username); 
    $email = stripslashes($_REQUEST ['email']);
    $email = mysqli_real_escape_string($con,$email);
    $password = stripslashes($_REQUEST ['password']);
    $password = mysqli_real_escape_string($con,$password);
    $password =  md5($password)
    $signupdate = date("Y-m-d H:i:s");

$Check_username_indb_query = "select * from users where username = ‘$username’ ";
$result_check = mysqli_query($con,$query);
If(mysqli_fetch_rows($result_check)==1)
{
               echo "Registration failed, this username is already taken please chose another one";
}
Else
{
$query = "INSERT into `users` (username, password, email, signupdate)
VALUES ('$username', '$password', '$email', '$signupdate')";
        $result = mysqli_query($con,$query);
        if($result)
         {
            echo "<div class='form'>
         <h3>You are registered successfully.</h3>
        <br/>
        Click here to <a href='login.php'>Login</a></div>";

   }

   ?>