php form submission to mysql database

2019-09-20 03:44发布

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','','','')"); 
} 

3条回答
等我变得足够好
2楼-- · 2019-09-20 04:11

Try the following Code

    include ("db.php"); 
    if (isset($_POST['register'])) 
    { 
    echo $name = ($_POST["name"]); 
    echo $email = ($_POST["email"]); 
    echo $uname = ($_POST["uname"]);
    echo $password = ($_POST["pass"]);
   $var = mysqli_query('SELECT * from company_profile where email_id = "'.$email.'" or username = "'.$uname.'" ');
$num = mysqli_num_rows($var);
if($num==0)
{
    $result = INSERT INTO company_profile(user_name, password, company_name, email, phone, country, activation_string) VALUES ('$uname','$password','$name','$email','','','');
    $res = mysqli_query($result);
        if($res)
        {
        echo "Records Inserted Successfully!!";
        }
        else
        {
        echo "Records Inserted Failed!!";
        }
}
else
{
echo "User with the Details Already exists!!"
}
     }
查看更多
男人必须洒脱
3楼-- · 2019-09-20 04:17

The query method would return true or false, depending on if the row has been inserted or not.

查看更多
欢心
4楼-- · 2019-09-20 04:29

*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.";
         }
     } 
}
  ?>
查看更多
登录 后发表回答