After reading many simple cases, I found that there are few posts about using PDO to implement simple login registration, or MySql and MySqli implementation, even if the implementation is not meaningful, js can do things, there is no need to put in PHP, but I just don’t believe that, keep a bit of a surly. Simple implementation of the login registration, please look at the code below, there are errors in the hope of the big guys to teach!
Create database name itcast, table name user, and execute the following code
Create table user(ID int unsigned primary key auto_increment, username varchar(10) not null COMMENT 'username ', Password char(20) not null comment 'password ', email varchar(40) not null comment' email ')charset=utf8; Insert into user (username,password,email) values (' 123456','[email protected]');Copy the code
The directory structure
Start by registering the foreground page register_html.php
<! <title> </title> </head> <form action="./register.php" Method ="post"> <table >< tr><td class="title" colspan="2"> </td></tr> <tr><th> User name: < / th > < td > < input type = "text" name = "username" / > < / td > < / tr > < tr > < th > email: < / th > < td > < input type = "text" name = "email" / > < / td > < / tr > < tr > < th > password: < / th > < td > < input type = "password" name = "password" id = "pw1" / > < / td > < / tr > < tr > < th > confirm password: </th><td><input type="password" id="pw2" /></td></tr> <tr><td colspan="2" class="td-btn"> <input type="submit" <input type="button" value=" return to login "class="button" onclick="location.href='login.php'" /> </td></tr> </table> </form> </html>Copy the code
Register background page register.php
<? php header ('Content-type:text/html; charset=utf-8'); $DSN = "mysql:host=localhost; mysql:host=localhost; dbname=itcast; charset=utf8"; $user = 'root'; $PWD = 'root'; Try {$pdo = new pdo ($DSN,$user,$PWD); // If the post form is not empty if(! Empty ($_POST)){$fields = array('username','password','email'); $values = array(); $values = array(); Foreach ($fields as $k=>$v){$data = isset($_POST[$v])? $_POST[$v] : ''; If ($data=='') die($v.') '); $fields[$k] = "$v"; $values[] = "'$data'"; $fields = implode(',', $fields); $fields = implode(',', $fields); $values = implode(',', $values); $values = implode(',', '); $SQL = "insert into user ($fields) values ($values)"; $SQL = "insert into user ($fields) values ($values)"; If ($res = $pdo->query($SQL)){echo '<script>alert(" pdo! ") ); window.location.href="login_html.php"; </script>'; }else{die (' Registration failed! '); } } }catch(PDOException $e){ echo $e->getMessage().'<br>'; echo $e->getLine().'<br>'; echo $e->__toString().'<br>'; } define('APP', 'itcast'); require './register_html.php';Copy the code
Log in to the foreground page login_html.php
<! DOCTYPE HTML > < HTML > <head> <meta charset=" utF-8 "> <title> Action = ". / login. PHP "> < table > < tr > < td > welcome login < / td > < / tr > < tr > < th > username: < / th > < td > < input type = "text" name = "username" / > < / td > < / tr > < tr > < th > password: < / th > < td > < input type = "password" name = "password" / > < / td > < / tr > < tr > < td > < input type = "submit" value = "login" / > < input Type = "reset" value = "to fill in" / > < / td > < / tr > < / table > < / form > < / body > < / HTML >Copy the code
Login to the background page login.php
<? php header ('Content-type:text/html; charset=utf-8'); $DSN = "mysql:host=localhost; dbname=itcast; charset=utf8"; $user = 'root'; $PWD = 'root'; $pdo = new pdo ($DSN,$user,$PWD); $pdo = new pdo ($DSN,$user,$PWD); // If the form is not empty if(! $username = isset($_POST['username'])? trim($_POST['username']) : ''; $password = isset($_POST['password']) ? ($_POST['password']) : ''; $SQL = "select 'id', 'password' FROM 'user' WHERE 'username' ='$username'"; If ($res = $pdo->query($SQL)){echo '<script>alert(" $pdo "); window.location.href="index.php"; </script>'; }else{// Otherwise a login failure is displayed. Die (' Login failed! '); }}catch(PDOException $e){echo $e->getMessage().'<br>'; echo $e->getLine().'<br>'; Echo $e->__toString().'<br>'; } define('APP', 'itcast'); require './login_html.php'; ? >Copy the code
Finally, we logged in to the inex.php page successfully
<! DOCTYPE HTML > < HTML > <head> <meta charset=" utF-8 "> <title>Insert title here</title> </head> <body> <h1> Login successful! </h1> </body> </html>Copy the code
Here is not very beautiful, you can add a nice style on this basis, add two password is the same judgment, use regular to achieve the registration format judgment and so on, here is not a talk, the main purpose is to achieve a simple login registration, hee hee!