html+PHP提交表单并保存到MySQL中

写了一个简单的收集信息的页面,页面上只有一个表单,用户将数据写入表单并提交之后,转到另一个页面并且将数据保存的MySQL中。


信息统计页面


<html>
<head>
<meta charset="utf-8">
<meta content="width=device-width,user-scalable=no" name="viewport">
<title>form</title>
<style type="text/css">
	.p1{
		color: black;
		font-size: 30px;
	}
	#box1{
		background-color: #f1861e;
		width: 75%;
		height: 60%;
		float: 0 auto;
		text-align: center;
	}
	#input1{
		width: 100px;
		height: 50px;
		margin: 20px;
	}
	#input0{
		width: 200px;
		height: 30px;
	}
</style>
</head>
<body>
<div style="text-align: center;">
 	<div id="box1">
		<form action="welcome.php" method="post">
			<b class="p1">姓名: </b><input type="text" name="name" id="input0"><br>
			<b class="p1">学号: </b><input type="text" name="num" id="input0"><br>
			<b class="p1">班级:</b><input type="text" name="classl" id="input0"><br>
			<input type="submit" value="提交" id="input1">
		</form>
	</div>
</div>

 
</body>
</html>

提交后跳转页面


phpmyadmin页面


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
	<title></title>
</head>
<body>
	<?php echo $_POST["num"]; ?><br>
	欢迎<?php echo $_POST["classl"]; ?> 
	<?php echo $_POST["name"]; ?>!
	<?php
		$file_path = "info.txt";
		if(file_exists($file_path)){
			$fp = fopen($file_path, "w");
			$str = $_POST["num"] . PHP_EOL . $_POST["classl"] .PHP_EOL . $_POST["name"];
			fwrite($fp, $str);
			
		}
		fclose($fp);
	?>
	<?php
			$mydbhost = "localhost";
			$mydbuser = "root";
			$mydbpass = 'helloworld';
			$conn = mysqli_connect($mydbhost, $mydbuser, $mydbpass);
			if(! $conn){
				die("connect error: " . mysqli_error($conn));
			}
			mysqli_select_db( $conn, 'person');
			$sql="INSERT INTO student (name, Sno, class)
			VALUES
			('$_POST[name]','$_POST[num]','$_POST[classl]')";
			$retval = mysqli_query($conn, $sql);
			if(! $retval){
				die("create error" . mysqli_error($conn));

			}
			mysqli_close($conn);
		?>
</body>
</html>


猜你喜欢

转载自blog.csdn.net/horizonhui/article/details/79916610