<link rel="stylesheet" type="text/css" href="../style/common.css">
<script type="text/javascript">
function check()
{
//验证商品名称
var stuname=document.getElementById('stuname');
if(stuname.value=='')
{
alert('名字不能为空');
stuname.focus(); //获得焦点
return false;
}
var stusex=document.getElementById('stusex');
if(stusex.value=='')
{
alert('性别不能为空');
stusex.focus();
return false;
}
var stuage=document.getElementById('stuage');
if(stuage.value==''|| isNaN(stuage.value))
{
alert('年龄必须是数字');
stuage.select(); //选择内容
return false;
}
var stuadd=document.getElementById('stuadd');
if(stuadd.value=='')
{
alert('地址不能为空');
stuadd.focus();
return false;
}
}
</script>
</head>
<body>
<?php
if(isset($_POST['button']))
{
//获取提交数据
$stuname=$_POST['stuname'];
$stusex=$_POST['stusex'];
$stuage=$_POST['stuage'];
$stuadd=$_POST['stuadd'];
//链接数据库
mysql_connect('localhost','root','')or die(mysql_error());
mysql_select_db('data');
mysql_query('set names utf8');
$sql="insert into stu values ('$stuname','$stusex','$stuage','$stuadd')";
//echo $sql;
if(mysql_query($sql))
{
echo '插入成功';
header('location:admin1.php');
}
else
{
echo '插入失败';
}
}
?>
<form name="form1" method="post" action="" onSubmit="return check()">
<table width="500" border="1" align="center">
<tr>
<th colspan="2">添加商品</th>
</tr>
<tr>
<td>人名</td>
<td><label for="stuname"></label>
<input type="text" name="stuname" id="stuname"></td>
</tr>
<tr>
<td>性别</td>
<td><label for="stusex"></label>
<input type="text" name="stusex" id="stusex"></td>
</tr>
<tr>
<td>年龄</td>
<td><label for="stuage"></label>
<input type="text" name="stuage" id="stuage"></td>
</tr>
<tr>
<td>地址</td>
<td><label for="stuadd"></label>
<input type="text" name="stuadd" id="stuadd"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="button" id="button" value="提交">
<input type="button" name="button2" id="button2" value="返回" onClick="location.href='admin1.php'"></td>
</tr>
</table>
</form>
如图,输出SQL语句是能返回值得,到了判断那里就不行了