Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a registration form. In the database, the username and email are unique index. When the form submits and username or email are already present in the database, the values are not inserted. I want to notify the user that the values were not inserted. How can i do this?

HTML

<form action="register.php" method="post" id="reg" onsubmit='return validate();'>
    Company Name: 
    <input type="text"  class="inputs" name="name" id="name" /><br />
    Email:
    <input type="text" class="inputs" name="email" id="txtEmail" /><br />
    User name:
    <input type="text"  class="inputs"  name="uname" id="uname"/><br />
    Password:
    <input type="password" class="inputs" name="pass" id="pass1"/><br />
    Conferm Password:
    <input type="password" class="inputs" name="cpass"  id="pass2"/><br /><br /> 
    <input type="submit" value="Register" class="button" />
</form>

register.php:

include ("db.php"); 
if (isset($_POST['register'])) { 
    echo $name = ($_POST["name"]); 
    echo $email = ($_POST["email"]); 
    echo $uname = ($_POST["uname"]); 
    echo $password = ($_POST["pass"]); 
    mysqli_query($con,"INSERT INTO company_profile(user_name, password, company_name, email, phone, country, activation_string) VALUES ('$uname','$password','$name','$email','','','')"); 
} 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
208 views
Welcome To Ask or Share your Answers For Others

1 Answer

*Sweet And Short *

First check that username or email is exist or not using select query if resulting is 0 (it means not exists), Insert query will run ahead

<?php 
   if($_POST['register']){   
      $uname = $_POST['uname'];
      $email = $_POST['email'];
      $name= $_POST['name'];
      $pass= $_POST['pass'];
      $result =  mysqli_query($con, 'SELECT * from TABLE_NAME where email_id = "'.$email.'" or username = "'.$uname.'" ');
       if(mysqli_num_rows($result) > 0){
          echo "Username or email already exists.";
       }else{
         $query = mysqli_query($con , 'INSERT INTO TABLE_NAME (`email_id`, `username`,`name`,`pass`) VALUES("'.$email.'", "'.$email.'", "'.$uname.'","'.$name.'", "'.$pass.'")');

         if($query){
            echo "data are inserted successfully.";
         }else{
          echo "failed to insert data.";
         }
     } 
}
  ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...