PHP连接数据库,将表单中的数据插入数据库,查询并显示数据库中的数据

最近学了一点关于PHP和数据库的东西,

 

index.php文件
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style type="text/css">
body {
	text-align: center;
	padding-top: 150px;	
}

p {
	text-align: center;
}
</style>
</head>

<body>
<h1>学生签到</h1>
<hr>
<form action="s_inform.php" method="post">

    学号:&nbsp;&nbsp;
    <input type="text" name="Sno"/><br/><br/>
    密码:&nbsp;&nbsp;
    <input type="password" name="password"/><br/><br/>
    时间:&nbsp;&nbsp;
    <input type="datetime" name="time"/><br/><br/>
    地点:&nbsp;&nbsp;
    <input type="text" name="place" /><br/><br/>
    <br />

    <input type="submit" name="sub_infrom" value="签到">
    <input type="button" name="button1" value="历史签到" onclick="window.location='qwe.php';">
    <input type="button" name="button2" value="注册" onclick="window.location='register.php';">
    
</form>
<p><a href="serve.html">@有问题联系我们</a></p>
</body>
</html>

 

register.php文件
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style type="text/css">
body {
	text-align: center;
	padding-top: 150px;
}

p {
	text-align: center;
}
body h2 {
	text-align: left;
	padding-left: 480px;
	font-size: 18px;
}

</style>
</head>

<body>
<h1>学生签到</h1>
<hr>
<h2>+注册新用户</h2>
    <form action="s_reg.php" method="post">
    学号:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="text" name="username" /><br/><br/>
    密码:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="password" name="password" /><br/><br/>
    确认密码:
    <input type="password" name="confirm" /><br/>
    <br />
    <input type="submit" name="reg" value="点击注册">
</form>



<p>@有问题联系我们</p>
</body>
</html>

 

inform.php文件
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<?php
	
	$user = $_POST["Sno"];
	$psw = $_POST["password"];
	$time = $_POST["time"];
	$place = $_POST["place"];
	
	if($user == "" || $psw == "" || $place == "" || $time=="")
	{
		echo "<script>alert('请确认信息完整性!'); history.go(-1);</script>";
	}
	else
	{
		mysql_connect("ip","root","密码");	//连接数据库
		mysql_select_db("qiandao");	//选择数据库
		mysql_query("set names utf8");//设置编码格式集
		$sql = "select Sno from S_reg where Sno = '$user'";	//SQL语句
		$result = mysql_query($sql);	//执行SQL语句
		$num = mysql_num_rows($result);	//统计执行结果影响的行数
		if($num==0)	//如果未存在该用户
		{
			echo "<script>alert('该学号未注册'); history.go(-1);</script>";
		}
		else	//存在当前注册用户名称
		{
			$sql_1 = "select password from S_reg where Sno = '$user' and password = '$psw'";	//SQL语句
			$result_1 = mysql_query($sql_1);	//执行SQL语句
			$num_1 = mysql_num_rows($result_1);	//统计执行结果影响的行数
			if($num_1==0)	//如果密码错误
			{
				echo "<script>alert('密码错误'); history.go(-1);</script>";
			}
			else
			{
				$sql_in = "insert into S_infrom (Sno,Place_qiandao,Time_qiandao) values ('$user','$place','$time')";
				$res_in = mysql_query($sql_in);
				//$num_insert = mysql_num_rows($res_insert);
				if($res_in)
				{
					echo "<script>alert('签到成功!'); history.go(-1);</script>";
				}
				else
				{
					echo "<script>alert('系统繁忙,请稍候!'); history.go(-1);</script>";
				}
			}
		}
	}

		

?>
</body>
</html>

 

s_reg.php文件
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>

<body>

<?php
	//if(isset($_POST["Submit"]) && $_POST["Submit"] == "注册")
	//{
		$user = $_POST["username"];
		$psw = $_POST["password"];
		$psw_confirm = $_POST["confirm"];
		if($user == "" || $psw == "" || $psw_confirm == "")
		{
			echo "<script>alert('请确认信息完整性!'); history.go(-1);</script>";
		}
		else
		{
			if($psw == $psw_confirm)
			{
				mysql_connect("ip","root","密码");	//连接数据库
				mysql_select_db("qiandao");	//选择数据库
				mysql_query("set names utf8");//设置编码格式集
				$sql = "select Sno from S_reg where Sno = '$user'";	//SQL语句
				$result = mysql_query($sql);	//执行SQL语句
				$num = mysql_num_rows($result);	//统计执行结果影响的行数
				if($num)	//如果已经存在该用户
				{
					echo "<script>alert('该学号已注册'); history.go(-1);</script>";
				}
				else	//不存在当前注册用户名称
				{
					$sql_insert = "insert into S_reg (Sno,password) values('$user','$psw')";
					$res_insert = mysql_query($sql_insert);
					//$num_insert = mysql_num_rows($res_insert);
					if($res_insert)
					{
						echo "<script>alert('注册成功!'); history.go(-1);</script>";
					}
					else
					{
						echo "<script>alert('系统繁忙,请稍候!'); history.go(-1);</script>";
					}
				}
			}
			else
			{
				echo "<script>alert('密码不一致!'); history.go(-1);</script>";
			}
		}
	/*}
	else
	{
		echo "<script>alert('提交未成功!'); history.go(-1);</script>";
	}*/
?>

</body>
</html>

 

qwe.php文件
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style type="text/css">
body {
	text-align: center;
	padding-top: 150px;
	background-image: url(image/1.jpg);
}
</style>
</head>
<body>
<center>
<?php

	$user = $_POST["username"];
	$psw = $_POST["password"];
	if($user == "" || $psw == "")
	{
		echo "<script>alert('请确认信息完整性!'); history.go(-1);</script>";
	}
	else
	{
		$con=mysql_connect("ip","root","密码");	//连接数据库
		mysql_select_db("qiandao");	//选择数据库
		mysql_query("set names utf8");//设置编码格式集
		$sql = "select Sno from S_reg where Sno = '$user'";	//SQL语句
		$result = mysql_query($sql);	//执行SQL语句
		$num = mysql_num_rows($result);	//统计执行结果影响的行数
		if($num==0)	//如果未存在该用户
		{
			echo "<script>alert('该学号未注册'); history.go(-1);</script>";
			mysql_close($con);
		}
		else	//存在当前注册用户名称
		{
			$sql_1 = "select password from S_reg where Sno = '$user' and password = '$psw'";	//SQL语句
			$result_1 = mysql_query($sql_1);	//执行SQL语句
			$num_1 = mysql_num_rows($result_1);	//统计执行结果影响的行数
			if($num_1==0)	//如果密码错误
			{
				echo "<script>alert('密码错误'); history.go(-1);</script>";
				mysql_close($con);
			}
			else
			{
			   
				$q="select * from S_infrom where Sno='$user'";//设置查询指令
				$result=mysql_query($q);//执行查询
				
				echo "<table border='1'>
					<tr>
					<th>学号</th>
					<th>签到地点</th>
					<th>签到时间</th>
					</tr>";
				
				while($row=mysql_fetch_assoc($result))//将result结果集中查询结果取出一条
				{
					 echo"<tr><td>".$row["Sno"]."</td>
					 <td>".$row["Place_qiandao"]."</td>
					 <td>".$row["Time_qiandao"]."</td></tr>";
				}	
				echo "</table>";
			}
		}
	}
	
?>

</center>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值