注册页面(index.html):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <span>注册:</span> <form action="index.php" method="post" style="margin-left: 50px;"> 名称:<input type="text" name="username"> <br> 密码:<input type="password" name="password"> <br> <button>注册</button> </form> <form action="test.html"> 跳转到:<button>登录</button> </form> </body> </html>
注册页面(index.php):
<?php // 账号 $user = $_POST['username']; // echo $user; // 密码 $pass = $_POST['password']; // echo $pass; // 判断账号密码是否为空 // 账号 if(empty($user) || empty($pass)){ // echo '$user is empty'; echo "<script>alert('请输入完整内容'); window.location.href = 'index.html'; </script>"; }else{ // 连接数据库 $servername = "localhost"; $username = "root"; $passwrod = "asdf1234"; $dbname = "denglu"; $conn = new mysqli($servername, $username, $passwrod, $dbname); if($conn -> connection_error){ die('连接失败'); }else{ // echo "连接成功"; } // 创建数据库 // $sql = "CREATE DATABASE denglu"; // if($conn -> query($sql) === TRUE){ // echo "数据库创建成功"; // }else{ // echo "数据库创建失败"; // } // 插入单挑数据 $sql = "SELECT username,password FROM denglu WHERE username = '$user'"; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_assoc($result); // var_dump($sql); // echo "<br>"; // var_dump($result); // echo "<br>"; // var_dump($row); if($row){ echo "该账号已存在"; }else{ $sqll = "INSERT INTO denglu (username, password) VALUES('$user', '$pass')"; // $result = mysqli_query($conn, $sqll); // var_dump($result); // $row = mysqli_fetch_assoc($result); // var_dump($row); if($conn->query($sqll)){ $resulf = [ 'code' =>1, 'msg' => '注册成功', ]; echo json_encode($resulf); }else{ $resulf = [ 'code' =>2, 'msg' => '注册失败', ]; echo json_encode($resulf); } } } ?>
注册页面(test.html):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <span>登录:</span> <form action="test.php" method="post" style="margin-left: 50px;"> 名称:<input type="text" name="username"> <br> 密码:<input type="password" name="password"> <br> <button>登录</button> </form> <form action="index.html"> 跳转到:<button>注册</button> </form> </body> </html>
注册页面(test.php):
<?php // 账户 $user = $_POST['username']; // echo $user; echo '<br>'; // 密码 $pass = $_POST['password']; // echo $pass; if(empty($user) || empty($pass)){ // echo '$user is empty'; echo "<script>alert('请输入完整内容'); window.location.href = 'test.html'; </script>"; }else{ // 连接数据库 $servername = "localhost"; $username = "root"; $passwrod = "asdf1234"; $dbname = "denglu"; $conn = new mysqli($servername, $username, $passwrod, $dbname); if($conn -> connection_error){ die('连接失败'); }else{ // echo "连接成功"; } // 查询数据 $sql = "SELECT username,password FROM denglu WHERE username = '$user'"; $result = mysqli_query($conn, $sql); // echo $sql;die; if($conn->query($sql)){ $row = mysqli_fetch_assoc($result); // var_dump($row); if($row['password'] == $pass){ $resulf = [ 'code' =>1, 'msg' => '成功', 'data' =>$row, ]; echo json_encode($resulf); }else{ $resulf = [ 'code' =>0, 'msg' => 'error', ]; echo json_encode($resulf); } }else{ echo "查询失败"; } } ?>