Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

html - php form submission to mysql database

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

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

1 Reply

0 votes
by (71.8m points)

*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.";
         }
     } 
}
  ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...