致力于网页设计制作教程:HTTP://WWW.ASP119.COM

当前位置:首页 > 网络编程 > PHP教程 > 正文

PHP基础教程 向表中添加记录

作者:××× 来源:本站 浏览:  添加日期:2010-4-6 20:04:03

<?
if(!$_POST[name])        //如果没有记录输入
{
?>
<html>
<head>
<title>学生档案管理系统——记录添加页</title>
</head.
<body>
<script language="javascript">
function Juge(theForm)
{
 if (theForm.name.value == "")
 {
  alert("请输入姓名!");
  theForm.name.focus();
  return (false);
 }
 if (theForm.s_id.value == "")
 {
  alert("请输入学号!");
  theForm.s_id.focus();
  return (false);
 }
}
</script>
<center>
<h1>学生档案管理系统——记录添加页</h1>
<a href="14-23.php">返回首页</a>
<table border=1>
<form action="<?echo $PATH_INFO?>" method="post" onsubmit="return Juge(this)">
<tr>
<td>输入姓名:</td>
<td><input name="name" type="text"></td>
</tr>
<tr>
<td>输入性别:</td>
<td>
<input type=radio name=sex value=男 checked>男
<input type=radio name=sex value=女>女
</td>
</tr>
<tr>
<td>出生日期:</td>
<td>
<select name=b_y>
<?
for($i=1980;$i<2004;$i++)       //循环输出出生年
echo "<option value=".$i.">".$i."\n";
?>
</select>年
<select name=b_m>
<?
for($i=1;$i<13;$i++)        //循环输出出生月
echo "<option value=".$i.">".$i."\n";
?>
</select>月
<select name=b_d>
<?
for($i=1;$i<32;$i++)        //循环输出出生日
echo "<option value=".$i.">".$i."\n";
?>
</select>日
</td>
</tr>
<tr>
<td>输入学号:</td>
<td><input name="s_id" type="text"></td>
</tr>
<tr>
<td>监护人姓名:</td>
<td><input name="parent" type="text"></td>
</tr>
<tr>
<td>家庭电话:</td>
<td><input name="phone" type="text"></td>
</tr>
<tr>
<td>家庭住址:</td>
<td><input name="address" type="text"></td>
</tr>
<tr>
<td colspan="2"><center><input type=submit value="确认提交">
<input type=reset value="重新选择"></center></td>
</tr>
</form>
</table>
<?
}
else
{
//以下内容为获取表单传递的变量
 $name=$_POST[name];
 $sex=$_POST[sex];
 $birthday=$_POST[b_y]."年".$_POST[b_m]."月".$_POST[b_d]."日";
 $s_id=$_POST[s_id];
 $parent=$_POST[parent];
 $phone=$_POST[phone];
 $address=$_POST[address];
 require "14-21.php";
 $link=mysql_connect($db_host,$db_user,$db_pass)or die("不能连接到服务器".mysql_error());
 mysql_select_db($db_name,$link);     //选择test数据库
 $sql="insert into $table_name (name,sex,birthday,s_id,parent,phone,address) values('$name','$sex','$birthday','$s_id','$parent','$phone','$address')";
 mysql_query($sql,$link);       //执行插入记录的SQL语句
?>
<html>
<head>
<title>学生档案管理系统——记录添加页</title>
</head>
<meta http-equiv="refresh" content="2; url=14-23.php">
<body>
已经成功添加记录,两秒后返回。
</body>
</html>
<?
}
?>