-2

I have made user registration & login system. But i have problems in my form, when new user signup it must provide first name, last name, email, phone and password. After signup if an other user signup with different phone number but same email it also got sign up. So i need to signup those user's who have different phone and email. In simply words i need to remove repetitions. My html code is as follow:

 <form action="#" method="post">

   <input class="form-control" type="text" placeholder="First Name" required name="fname" >
   <input class="form-control" type="text" placeholder="Last Name" required   name="lname" >
   <input class="form-control" type="text" placeholder="Enter your phone number" 
   id="phonecheck"name="phone" 
 <input class="form-control" type="password" placeholder="Enter Password" required  name="password" >
<button class="btn btn-primary" type="submit" name="btnreg"><i class="fa fa-check-square"></i> Sign Up</button>

                        </form>

My php code:

<?php if (isset($_POST['btnreg'])) {
     include 'connection.php';


      $fname=$_POST['fname'];
      $lname=$_POST['lname'];
      $phone=$_POST['phone'];
      $email=$_POST['email'];
      $password=$_POST['password'];
        $sql=mysqli_query($con,"select * from tblusers where phone='".$phone."' AND email='".$email."'");

        $numrows=mysqli_num_rows($sql);

        if ($numrows==0) 
          {

             $sql="insert into tblusers (f_name,l_name,phone,email,password) values ('$fname','$lname','$phone','$email','$password') ";
             $result=mysqli_query($con,$sql);
              if ($result==true) {


                 echo "<script type = \"text/javascript\">
                                        alert(\"Successfully Registered.\");
                                        window.location = (\"login.php\")
                                        </script>";

                              }
              else {
                echo "<script type =text/javascript>
                                        alert(Registration Failed. Try Again);
                                        window.location = ('signup.php')
                                        </script>";
                   }
          }




      else  {
            echo "Email & Phone Number Already exists";
            }



        }

?>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Azam Khan
  • 9
  • 9
  • 2
    Your HTML code is probably the least relevant thing here. If you want to prevent users from registering twice with the same e-mail address - then put a UNIQUE INDEX on that column in your database, and handle the resulting insertion error accordingly, when it occurs. – 04FS Jan 28 '20 at 11:47
  • You need OR, not AND in your SQL query – Your Common Sense Jan 28 '20 at 12:24
  • @Your Common Sense thanks so much – Azam Khan Jan 28 '20 at 12:43
  • Does this answer your question? [How do I specify unique constraint for multiple columns in MySQL?](https://stackoverflow.com/questions/635937/how-do-i-specify-unique-constraint-for-multiple-columns-in-mysql) – Dharman Jan 29 '20 at 21:20

1 Answers1

-1

To do this, you need to check the existence of the data in the database in the php code. Check and if it does not exist, insert, or on the contrary, INSERT VALUES WHERE NOT EXISTS