<?php
$db = mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("school") or die(mysql_error());
if ($_POST['submit']){
$sql = "INSERT INTO student (id,name) VALUES ($id,'$name')";
$result = mysql_query($sql);
}
//删除
if($_POST['delete']){
$sql ="delete from student where id = $id";
$result = mysql_query($sql);
}
//修改
if($_POST['update']){
$sql = "update student set name='$name' where id = $id";
$result = mysql_query($sql);
}
$result = mysql_query("select * from student",$db);//查询
echo "<table border=1>\n";
echo "<tr><td>学号</td><td>姓名</td>";
echo "</tr>\n";
while ($myrow = mysql_fetch_row($result)){
printf("<tr><td>%s</td><td>%s</td>", $myrow[0], $myrow[1]);
}
echo "</table>\n";
?>
<html>
<form action="department.php" name="">
学号<input type="text" name="id"/> <br/>
姓名<input type="text" name="name"/><br/>
<input type="submit" value="添加" name="$_POST['submit']"/>
<input type="submit" value="删除" name="$_POST['delete']"/>
<input type="submit" value="修改" name="$_POST['update']"/>
</form>
</html>
$db = mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("school") or die(mysql_error());
if ($_POST['submit']){
$sql = "INSERT INTO student (id,name) VALUES ($id,'$name')";
$result = mysql_query($sql);
}
//删除
if($_POST['delete']){
$sql ="delete from student where id = $id";
$result = mysql_query($sql);
}
//修改
if($_POST['update']){
$sql = "update student set name='$name' where id = $id";
$result = mysql_query($sql);
}
$result = mysql_query("select * from student",$db);//查询
echo "<table border=1>\n";
echo "<tr><td>学号</td><td>姓名</td>";
echo "</tr>\n";
while ($myrow = mysql_fetch_row($result)){
printf("<tr><td>%s</td><td>%s</td>", $myrow[0], $myrow[1]);
}
echo "</table>\n";
?>
<html>
<form action="department.php" name="">
学号<input type="text" name="id"/> <br/>
姓名<input type="text" name="name"/><br/>
<input type="submit" value="添加" name="$_POST['submit']"/>
<input type="submit" value="删除" name="$_POST['delete']"/>
<input type="submit" value="修改" name="$_POST['update']"/>
</form>
</html>
<form action="department.php" name="">
学号<input type="text" name="id"/> <br/>
姓名<input type="text" name="name"/><br/>
<input type="submit" value="添加" name="$_POST['submit']"/>
<input type="submit" value="删除" name="$_POST['delete']"/>
<input type="submit" value="修改" name="$_POST['update']"/>
</form>
</html>
改为
<html>
<form action="department.php" name="">
学号<input type="text" name="id"/> <br/>
姓名<input type="text" name="name"/><br/>
<input type="submit" value="添加" name="submit"/>
<input type="submit" value="删除" name="delete"/>
<input type="submit" value="修改" name="update"/>
</form>
</html>这样$_POST['submit']才有值