PHP学习笔记(四 实训结束) 实现登录与注册连接数据库

文章目录

    • 知识点
      • 数据库连接
      • 页面停顿跳转
      • 获取数据
    • login.html
    • login.php
    • register.html
    • register.php

知识点

数据库连接

	error_reporting(E_ALL & ~E_DEPRECATED);
	header('Content-type:text/html;charset=utf8');
	$conn = mysql_connect('localhost','root','root')
	or die("");
	mysql_set_charset('utf8');//5.5以下适用
	$select = mysql_select_db('user')
	or die("");

页面停顿跳转

	echo '';

获取数据

	$query = mysql_query($sql)
	or die("");
	$t = mysql_fetch_array($query,MYSQL_ASSOC);//此时$t是一个数组

login.html

<html>
	<head>
		
		<title>
			登录
		title>
	head>
	
<div>
用户名:<br> 密码:<br> form> div> div> html>

login.php


	$name = $_POST['name'];
	$password = $_POST['password'];
 	error_reporting(E_ALL & ~E_DEPRECATED);
	header('Content-type:text/html;charset=utf8');
	$conn = mysql_connect('localhost','root','root')
	or die("");
	mysql_set_charset('utf8');//5.5以下适用
	$select = mysql_select_db('user')
	or die("");
	$sql = "select * from user where name = '{
       $name}'";
	$insert_sql = "insert into student values ('default','张三','23')";
	$update_sql = "update student set name = '李四' where id = 7";
	$delete_sql = "delete from student where id = 7";
	$query = mysql_query($sql)//此时$query是一个数组
	or die("");
	$t = mysql_fetch_array($query,MYSQL_ASSOC);
	// print_r($t);
	if($t['password'] == $password){
     
		echo "登录成功";
	}else{
     
		echo "用户名或密码错误";
	}
?>

register.html

<html>
	<head>
		
		<title>
			注册
		title>
	head>
	
<div> 用户名:<br> 密码:<br> 确认密码:<br> 邮箱:<br> form> div> div> html>

register.php


	$name = $_POST['name'];
	$password = $_POST['password'];
	$email = $_POST['email'];
	$repassword = $_POST['repassword'];
 	error_reporting(E_ALL & ~E_DEPRECATED);
	header('Content-type:text/html;charset=utf8');
	$conn = mysql_connect('localhost','root','root')
	or die("");
	mysql_set_charset('utf8');//5.5以下适用
	$select = mysql_select_db('user')
	or die("");
	$sql = "select * from user where name = '{
       $name}'";
	$insert_sql = "insert into user values('{
       $name}','{
       $password}','{
       $email}')";
	$update_sql = "update student set name = '李四' where id = 7";
	$delete_sql = "delete from student where id = 7";
	$query = mysql_query($sql)//此时$query是一个数组
	or die("");
	// $t = mysql_fetch_array($query,MYSQL_ASSOC);
	// print_r($t);s
	if(empty($password)){
     
		echo "密码不能为空";	
		echo '';
	}elseif(empty($name)){
     
		echo "用户名不能为空";
		echo '';
	}elseif(empty($email)){
     
		echo "邮箱不能为空";
		echo '';
	}elseif($password != $repassword){
     
		echo "两次密码不一致";
		echo '';
	}else{
     
		echo "注册成功";
		echo '';
		mysql_query($insert_sql)
		or die("");//此时$query是一个数组	
	}
?>

你可能感兴趣的:(php)