用php+mysql写一个网站注册实例

<?php
$pdo = new PDO('mysql:host=localhost;dbname=iapp', 'iapp' , 'iapp' , array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8';"));
if (!empty($_COOKIE['name'])) {
    echo '<script>window.location.href="/index.php";</script>';
}
	if(!empty($_POST)){
		$name = $_POST['name'];
		if(empty($name)){
			echo "请输入昵称";
			exit;
		}
		$password = $_POST['password'];
		if(empty($password)){
			echo "请输入密码";
			exit;
		}
		$account = $_POST['account'];
		if(empty($password)){
			echo "请输入账号";
			exit;
		}
	$stmt = $pdo->prepare('SELECT * FROM user WHERE `account`="'.$_POST['account'].'"');
    $stmt->execute();
    $user= $stmt->fetchAll();
    $find = $user[0];
    if(!empty($find)){
      echo '<script>window.alert("账号已经被注册");history.back();</script>';
      return false;	
			      }
		$stmt = $pdo->prepare('SELECT * FROM user WHERE `name`="'.$_POST['name'].'"');
    $stmt->execute();
    $user= $stmt->fetchAll();
    $find = $user[0];
    if(!empty($find)){
      echo '<script>window.alert("昵称已经被注册");history.back();</script>';
      return false;	
			      }		      
		$date = date('Y-m-d');
		$vip = 'no';
	try{
 			$sql = "INSERT INTO user(`name`,`account`,`password`,`date`,`vip`)VALUES('{$name}','{$account}','{$password}','{$date}','{$vip}');";
 		 	$stmt = $pdo->prepare($sql);
 			$stmt->execute();
 		 echo '<script>window.alert("注册成功");window.location.href="/denglu.php";</script>';
 		 setcookie('name',$name,time()+3600,'/');
 		}catch (PDOException $e){
 			echo "注册失败";
 		}
      	return false;
      	}
?>


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>	</title>
</head>
<body>
    <div style="width:400px;height:400px;background:url(https://scpic.chinaz.net/files/pic/pic9/202111/apic36436.jpg);margin:0 auto;margin-top:100px;padding-top:180px;">
        <center>
	<form method="post" action=""> 	
<div>昵称</div>
<input type="text" name="name"></input>
<div>账号</div>
<input type="text" name="account"></input>
<div>密码</div>
<input type="text" name="password"></input>
<div style="margin-top:5px;"><button>注册</button></div>
	</form></center>
	</div>
</body>
</html>

 

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片