数据库里的表和内容
----------------------------------------------------------------------------------
SET FOREIGN_KEY_CHECKS=0; 
-- ---------------------------- 
-- Table structure for test 
-- ---------------------------- 
CREATE TABLE `test` ( 
`id` int(11) NOT NULL auto_increment, 
`name` tinytext NOT NULL, 
`pid` tinytext NOT NULL, 
PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=gbk; -- ---------------------------- 
-- Records 
-- ---------------------------- 
INSERT INTO `test` VALUES ('3', 'names', '0'); 
INSERT INTO `test` VALUES ('5', 'fdsafdsf', '0'); 
INSERT INTO `test` VALUES ('9', 'dad', '3'); 
INSERT INTO `test` VALUES ('10', 'fs', '5'); 
INSERT INTO `test` VALUES ('11', 'fsd', '3:9'); 
INSERT INTO `test` VALUES ('12', '发生大上', '3:9'); 
INSERT INTO `test` VALUES ('13', '防撒', '5'); 
INSERT INTO `test` VALUES ('14', '5435', '5'); 
INSERT INTO `test` VALUES ('15', '5435235', '5'); 
INSERT INTO `test` VALUES ('16', '高低杠', '5:15'); 
INSERT INTO `test` VALUES ('17', '特务', '5:15:16'); 
INSERT INTO `test` VALUES ('18', '后果反对恢复的', '5:15:16:17'); 
INSERT INTO `test` VALUES ('19', '恢复大会', '3:9:12'); 
INSERT INTO `test` VALUES ('20', '脚后跟飞机', '5:14'); 
INSERT INTO `test` VALUES ('21', '经过', '5:14'); 
INSERT INTO `test` VALUES ('22', '犯得上', '5:13'); 
INSERT INTO `test` VALUES ('23', '后果反对', '3:9:11'); 
INSERT INTO `test` VALUES ('24', '他', '3:9:11'); 
----------------------------------------------------------------------------------代码部分
----------------------------------------------------------------------------------
<?php 
$conn = mysql_connect('127.0.0.1','root','111111') or die('连接失败:' . mysql_error()); 
mysql_query('SET NAMES GBK',$conn); 
mysql_select_db("mytest"); 
if($_POST['submit']=="提交"){ 
if(empty($_POST['name'])){ 
exit(); 

$sql = "insert test (name,pid) values ('$_POST[name]','0')"; 
if(mysql_query($sql)){ 
echo '<script>alert("数据添加成功!");window.location="http://127.0.0.1/name.php";</script>'; 
exit(); 


if($_POST['button2']=="添加"){ 
$pid = trim($_POST['pid']); 
if(empty($pid)){ 
echo '<script>alert("父ID不能为空!");window.location="http://127.0.0.1/name.php";</script>'; 
exit(); 

$sql = "select * from test where id = " . $pid; 
$result = mysql_query($sql); 
$info = mysql_fetch_array($result); 
if(!$info){ 
echo '<script>alert("数据错误!");window.location="http://127.0.0.1/name.php";</script>'; 
exit(); 
}else{ 
if(trim($info['pid'])!='0'){ 
$pid = $info['pid'] . ':' . $pid; 

$sql = "insert test (name,pid) values ('$_POST[textfield]','$pid')"; 
echo $sql; 
$result = mysql_query($sql,$conn); 
if($result){ 
echo '<script>alert("子分类添加成功!");window.location="http://127.0.0.1/name.php";</script>'; 
exit(); 
}else{ 
echo '<script>alert("子分类添加失败,请检查代码");window.location="http://127.0.0.1/name.php";</script>'; 
exit(); 



?> <?php 
if($_GET['action']==""){ 
?> 
<table width="300" border="1" cellspacing="2" cellpadding="0"> 
<form name="form1" method="post" action=""> 
<tr> 
<td colspan="2" align="center">增加大类</td> 
</tr> 
<tr> 
<td width="115" align="right">名称:</td> 
<td width="185"><input type="text" name="name" id="name"></td> 
</tr> 
<tr> 
<td colspan="2" align="center"><input type="submit" name="submit" id="button" value="提交"></td> 
</tr> 
</form> 
</table> 
<?php 
}else{ 
?> 
<table width="500" border="1" cellspacing="2" cellpadding="0"> 
<form action="" method="post"> 
<?php 
$pid = trim($_GET['pid']); 
$sql = "select * from test where id = " . $pid; 
$result = mysql_query($sql,$conn); 
$info = mysql_fetch_array($result); 
?> 
<tr> 
<td colspan="2" align="center">增加子分类</td> 
</tr> 
<tr> 
<td width="150" align="right">父名称:</td> 
<td width="350"><?php echo $info['name'];?></td> 
</tr> 
<tr> 
<td align="right">名称:</td> 
<td><input type="text" name="textfield" id="textfield" /></td> 
</tr> 
<tr> 
<td align="right"> </td> 
<td> 
<input type="submit" name="button2" id="button2" value="添加" /> 
<input name="pid" type="hidden" value="<?php echo $pid;?>" /> 
</td> 
</tr> 
</form> 
</table> 
<?php 

?> 
<table width="500" border="1" cellspacing="3" cellpadding="0"> 
<?php 
function lode($temp){ 
$sum = ""; 
$num = count(explode(":",$temp)); 
for($i=1;$i<=$num;$i++){ 
$sum = $sum . " "; 

return $sum; 
} function cls($pid){ 
$SQL = "select * from test where pid = " . $pid; 
$Result = mysql_query($SQL); 
while($Info = @mysql_fetch_array($Result)){ 
echo ' <tr>'."\r\n"; 
echo ' <td width="350">'.lode($Info['pid']).$Info['name'].'--'.$Info['pid'].":".$Info['id'].'</td>'."\r\n"; 
echo ' <td width="150"><a href="?action=add&pid='.$Info['id'].'">增加子分类</a></td>'."\r\n"; 
echo ' </tr>'."\r\n"; 
$pid = $Info['pid'].":".$Info['id']; 
cls($pid); 

} $sql = "select * from test where pid = '0'"; 
$result = mysql_query($sql); 
while($info=mysql_fetch_array($result)){ 
//echo $info['name']."-<a href='?action=add&pid=".$info['id']."'></a><br>\r\n"; 
?> <tr> 
<td width="350"><?php echo $info['name'];?></td> 
<td width="150"><a href="?action=add&pid=<?php echo $info['id'];?>">增加子分类</a></td> 
</tr> 
<?php cls($info['id']);?> 
<?php 

?> 
</table> 
----------------------------------------------------------------------------------递归显示顺序有很大问题,没搞明白是那里错误了