简体   繁体   English

PHP表单提交到MySQL数据库

[英]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 的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: 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','','','')"); 
} 

* 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 首先检查用户名或电子邮件是否存在,如果结果为0(表示不存在),则使用select查询,将继续运行Insert查询

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

查询方法将返回true或false,这取决于是否已插入行。

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!!"
}
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM