PHP verification user name re registration [novice]

Posted by maxime on Mon, 02 Dec 2019 18:12:07 +0100

This summary uses the simplest way to verify whether the form submission (user name) exists in the data table through PHP. If it exists, it shows that it has been registered. If it does not exist, it can be registered normally.

The detailed code is as follows:

Steps omit the establishment of database and data table.

The front-end submission page code is as follows:

register.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Duplicate authentication of user name</title>
  </head>
  <body>
    <form action="register.php" method="post">
      <!-- action Point to submission page method For submission method -->

      <label>User name:<input type="text" name="username"/></label> <br />
      <button type="submit">register</button>
      <!-- The registration button here is used<input type="submit">Label and<button>All labels are OK -->
    </form>
  </body>
</html>

Now that you have completed the front-end registration page, start writing the PHP acceptance page:

register.php

<?php
include('conn.php');
//PHP Link database code, here I use the created separate connection page'conn.php',Code attached below

mysql_query("set character set 'utf8'");//Read Library 
mysql_query("set names 'utf8'");//Write library 
//These two lines of code can ensure that the content submitted from the web page to the database is displayed in Chinese format without disorderly code. If it is registered, it can only write a section of "write library"

//Query statement to help query whether the current registered user name exists in the database
$sql = "select * from T_USER where username='$_POST[username]'";
//First'username'Existing in the database username Value, compare it to the second'POST'Method passed username Value comparison

$rs = mysql_query($sql);
if(mysql_num_rows($rs)>0)//If the same user name exists in the database, then'$rs'The received variable is'true'So if greater than 1 is true, return'User name already exists'
{
    echo "User name already exists, please register again!";
    echo "<a href=register.php>[register]</a>";
} else //Otherwise, it can be successfully registered and submitted
{
   $sql="INSERT INTO T_HOSPITAL (username) VALUES ('$_POST[username]')";
if (!mysql_query($sql))
    {
      die('Error: ' . mysql_error());
    }
echo "<span>Registration succeeded!</span>";//Show registration success information
header("Refresh:1;url=login.php");//Refresh in a second to enter the login page
}
?>

PHP linked database page:

conn.php:

<?php
 $conn = mysql_connect("127.0.0.1","root","") or die("Database link error".mysql_error());
 mysql_select_db("DB",$conn) or die("Database access error".mysql_error());
 mysql_query("set names gb2312");
?>

Topics: PHP Database SQL IE