注册+登陆(增删改查)PHP+MYSQL+SESSION。。。+(核心解释在我的博客内,切记,一定要看!!!!!!!!!)

//sql语句页面

create table users(
id int(10) unsigned not null auto_INCREMENT KEY,
username varchar(16) not null,
password varchar(40) not null
)charset=utf8;
create table items(
id int(10) unsigned not null auto_increment key,
title varchar(50) not null,
mx tinytext NOT NULL,
ksdate date,
wcdate date,
user_id int(10) unsigned not null,
constraint  foreign key(user_id)  references users(id)
)charset=utf8;

//开始页面

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	请问一下帅哥贵你要:<br /><a href="denglu.php">登录</a>/<a href="zhuc.php">注册</a>
	
</body>
</html>

//注册页面

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">

</head>
<body>
	<?php
		//判断如果存在所的id代表还在登陆状态还没听退出呢,无论在哪一个页面中
		@session_start();
		if(isset($_SESSION['userid']))
		{
			require_once('denglucg.php');
		}
		else
		{
			if(isset($_POST['username'])&&(isset($_POST['password']))&&isset($_POST['password-confirm'])){
				if ($_POST['password']==$_POST['password-confirm']) {
					$db=new mysqli('localhost','root','','bb');
					$db->set_charset('utf8');
					if($db->connect_errno)
					{
						echo "抱歉,您的两次输入的密码不一致,请仲裁输入哈";
						exit();
					}

					else
					{
						$username = $_POST['username'];
						$password = $_POST['password'];
						//然后是要插入到数据库里,保存好哈
						$query = "INSERT INTO users VALUES(NULL,'".$username."','".sha1($password)."')";
					$result=$db->query($query);
					if($result)//如果登记插入成功了就
					{
						//插入的id赋值给userid,why,因为接下来要写清单,因为你注册了就代表你已经是改清单的主人了哈
							$_SESSION['userid'] = $db->insert_id; 
							require_once('denglucg.php');
							//这里的意思是只要每天退出就显示这里id内容哈
					}
					else
					{
						echo "登记没有成功哈";
					}
					$db->close();
					}
					}
			else {
					echo "好像你的确认密码有误";
					echo "<form action='zhuc.php' method='post'>";
					echo "<label>请选择一个用户名<input type='text' name='username'/></label>";
					
					echo "<label>请选择一个密码<input type='password' name='password'/></label>";
					echo "<label>请确认你的密码<input type='password' name='password-confirm'/></label>";
					echo "<input type='submit' value='登记'/>";
					echo "</form>";
					echo "<a href='denglu.php'>你已经是会员了吗?</a>";
				}
				}else
				{
			

		echo "<h1>注册页面</h1>";
		echo "<form action='zhuc.php' method='post'>";
		echo "<label>请选择一个用户名<input type='text' name='username'/></label>";
		echo "<label>请输入你的密码<input type='password' name='password'/></label>";
		echo "<label>请再次输入你的密码<input type='password' name='password-confirm'/></label>";
		echo "<input type='submit' value='登记'/>";
		echo "</form>";
	}

}

	?>
</body>
</html>

//登陆页面

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>	<link rel="stylesheet" type="text/css" href="style.css">

</head>
<body>
	<?php
	@session_start();
	//判断获取一下如果写了用户名与密码就可以连接数据库了,为什么,因为写了才可以判断该用户是否注册啊,然后指定字符编码utf8,为什么,因为这样可以写中文把唉,然后判断是否连接成功。为什么要这这样判断,因为我们是看不到的,得需要判断才知道啊
	if(isset($_POST['username'])&&(isset($_POST['password'])))//为什么获取在里面,因为存在是个函数啊,存在才获取啊,不存在获取什么,因为获取在存在的话,我做事喜欢有把握的事啊
	{
		$db=new mysqli('localhost','root','','bb');
		$db->set_charset('utf8');//设置字符编码
		if($db->connect_errno)
		{
			echo "没有连接数据库成功哈";
		}
		else
		{
			//连接成功了就判断是否是注册过的哈
			//要判断得获取哈
			$username=$_POST['username'];
			$password=$_POST['password'];
			//然后是查询一下,why,因为要知道数据库里面是否存在该名+密码的用户哈,看看存在就成功登陆了哈
			$query="SELECT id,username from users where username = '".$username."' and password = '".sha1($password)."'";
			
			//然后是运行哈
			$result=$db->query($query);
		//因为计算机不能说话,所以须要看看查询的语句里面有没有数据哈
		if($result->num_rows)//查询出来的结果他的属性里面有没有数据(字段)
		{
			echo "登陆成功,欢迎您";
			echo "请问你还想写吗?不想直接登录就能直接看到上次的信息啦";
			//我想存到服务器里面。没有退出的情况下就一直这样呈现登陆状态,否则重新登陆
			$row=$result->fetch_assoc();
			//这样是代表形成关联数组,也就是下标是字母哈,为什么,因为数据库;里面的字段名都是字母哈
			$_SESSION['username']=$row['username'];
			$_SESSION['userid']=$row['id'];
			//然后是对象释放数据
			$result->free();
		}
		else
		{
			echo "登陆失败,建议您您的输入有问题,或者没有注册";
		}
		$db->close();
		}
	
	if(isset($_SESSION['userid'])) {
			require_once('denglucg.php');
		}
	}
		else
		{
		echo "<form action='denglu.php' method='post'>";
		echo "<label>请输入您的用户名:<input type='text' name='username' /></label>";
		echo "<label>请输入您的密码:<input type='password' name='password' /></label>";
		echo "<input type='submit' value='登陆'>";
		echo "</form>";
	}
	?>

	aaa
</body>
</html>

//登陆后成功

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
	<?php
		//记住信息,并且屏蔽掉错误信息
		@session_start();//因为如果存在id的话,代表登录了还没有退出就保持这个页面的信息
		if (isset($_SESSION['userid'])) {
			//这里赋值给变量,为什么,因为每次都调用的话,就太消耗性能了呀
			$current_userid=$_SESSION['userid'];
			//然后就查询信息,查询哪一个用户到底在清单表里面写了些什么
			$query = "SELECT id, title,mx,ksdate,wcdate from items WHERE user_id = $current_userid";//当登录会员所有的信息//这里的意思是当登录的用户还没有退出的情况下,与users里面的id相匹配的话,就显示出来登录的用户所写的清单
			//然后链接数据库,;链接出来所谓的清单
			$db=new mysqli('localhost','root','','bb');
			$db->set_charset('utf8');
			//看看链接成不成功,
			if ($db->connect_errno) {
				echo "这次链接有问题。,重新登录把";
				exit();
			}
			  else {
				
				if(isset($_POST['completed-item'])) {
					$item_id = $_POST['completed-item'];
					$completed_query = "UPDATE items set wcdate= '".date("Y-m-d")."' WHERE id = $item_id";
					$completed_result = $db->query($completed_query);
					if(!$completed_result) {
						echo "没有更新";
						exit();
					}
				}
				
				if(isset($_POST['title'])) {
					$title = $_POST['title'];
					$description = $_POST['description'];
					$complete_by = $_POST['complete-by'];
					$user_id = $_SESSION['userid'];
					$stmt = $db->prepare("INSERT INTO items VALUES (?,?,?,?,?,?)");
					$id = NULL;
					$completed_on = NULL;
					$stmt->bind_param("issssi", $id, $title, $description, $complete_by, $completed_on, $user_id);
					$stmt->execute();
					if(!$stmt->affected_rows) {
						echo "没有插入";
					}
				}
				
				if(isset($_POST['item-to-delete'])) {
					$item_to_delete = $_POST['item-to-delete'];
					$delete_query = "DELETE FROM items WHERE id = $item_to_delete";
					$delete_result = $db->query($delete_query);
					if(!$delete_result) {
						echo "没有删除";
						exit();
					}
				}
				
				$result = $db->query($query);
				if($result->num_rows) {
					echo "<table>";
					echo "<caption>我的To-Do List</caption>";
					echo "<thead>";
					echo "<tr>";
					echo "<th>号</th>";
					$result->field_seek(1);
					while($field = $result->fetch_field()) {
						echo "<th>".$field->name."</th>";
					}
					echo "<th>删除</th>";
					echo "</tr>";
					echo "</thead>";
					for($i = 1; ($row = $result->fetch_assoc()); $i++) {
						echo "<tr>";
						echo "<td>$i</td>";
						echo "<td>".$row['title']."</td>";
						echo "<td>".$row['mx']."</td>";
						echo "<td>";
						if(!$row['ksdate']) {
							echo "无最后期限";
						} else {
							echo $row['ksdate'];
						}
						echo "</td>";
						if(!$row['wcdate']) {
							echo "<td>";
							echo "<form action='denglucg.php' method='post'>";
							echo "<input type='hidden' value='".$row['id']."' name='completed-item' />";
							echo "<input type='submit' value='标记为完成了'/>";
							echo "</form>";
							echo "</td>";
						} else {
							echo "<td>".$row['wcdate']."</td>";
						}
						echo "<td>";
							echo "<form action='denglucg.php' method='post'>";
							echo "<input type='hidden' name='item-to-delete' value='".$row['id']."'/>";
							echo "<input type='submit' value='删除'/>";
							echo "</form>";
						echo "</td>";
						echo "</td>";
						echo "</tr>";
					}

				}
				echo "</table>";
				echo "<br/>";
				echo "<form action='denglucg.php' method='post'>";
				echo "<h2>创建新事项</h2>";
				echo "<label><span>题目</span><input type='text' name='title'></label>";
				echo "<label><span>最后期限</span><input type='text' name='complete-by' value='YYYY-MM-DD'></label>";
				echo "<label><span>描写</span><textarea name='description'></textarea></label>";
				echo "<input type='submit' value='创建新事项'/>";
				echo "</form>";
			}
			echo "<a href='signout.php'>退出</a>";
			
		} else {
			echo "<a href='index.php'>在这里登录</a>";
		}
		
	?>
</body>
</html>

//退出页面

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>	<link rel="stylesheet" type="text/css" href="style.css">

</head>
<body>
	<?php
session_start();
		if(isset($_SESSION['userid'])) {
			session_destroy();
			echo "你已经推出了";
		} else {
			echo "你还没有登录呢";
		}
		echo "<br/>";
		echo "<a href='index.php'>你想登录吗?</a>";
	?>
</body>
</html>
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贵哥的编程之路(热爱分享)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值