码迷,mamicode.com
首页 > 数据库 > 详细

数据库添加数据

时间:2016-06-27 19:33:03      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

/*insert.php*/

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
	<title>insert</title>
</head>
	<center>
		<body>
			<form name="from1" action="insert.php" method="post">
				<table border="1" width="30%">

					<tr>
						<td colspan="2" align="center">添加学生</td>
					</tr>

					<tr>
						<td align="center">编号</td>
						<td><input type="text" name="id"></td>
					</tr>

					<tr>
						<td align="center">姓名</td>
						<td><input type="text" name="name"></td>
					</tr>

					<tr>
						<td align="center">性别</td>
						<td>
							<input type="radio"  name="sex" value="男"  checked=‘checked‘>男
							<input type="radio"  name="sex" value="女">女
						</td>
					</tr>

					<tr>
						<td align="center">年龄</td>
						<td>
							<select name="age">
								<?php
								for($i=10;$i<30;$i++)
								{
								?>
									<option value="<?php echo $i?>"><?php echo $i?>岁</option>
								<?php
								}
								?>
							</select>
						</td>
					</tr>

					<tr>
						<td colspan="2" align="center"><input name="Submit" type="submit" value="保存">
						<a href=‘sy.php‘><input name="return" type="button" value="返回" ></a></td>
					</tr>
				</table>	
			</form>
		</body>
	</center>
</html>

<?php

//检查是否正在提交表单
if($_SERVER[‘REQUEST_METHOD‘]=="POST"){

$conn=mysql_connect("localhost","root","root");

$select_db=mysql_select_db("test");

$name=$_POST[‘name‘];
$id=$_POST[‘id‘];
$sex=$_POST[‘sex‘];
$age=$_POST[‘age‘];

mysql_query("set names utf8");

$insert=mysql_query("insert into stu (stu_id,stu_name,stu_age,stu_sex) values (‘$id‘,‘$name‘,‘$age‘,‘$sex‘)");
//往数据库中添加数据

if($insert)
	{
		//echo "<script>alert(‘Insert seccssed!‘)</script>"."<br>";
		//echo "添加成功!";
		header("location:sy.php");
	}
else 
	{
		echo "出错了!<br>";
		die(mysql_error());
		//echo "<script>alert(‘insert wrong!‘)</script>"."<br>";
	}
mysql_close($conn);

}

?>

 

数据库添加数据

标签:

原文地址:http://www.cnblogs.com/-beyond/p/5621235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!