php课程---练习(联系人信息表)

做一个联系人表,实现增删改功能

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>主页面</title>
</head>

<body>
<?php
    $db =new mysqli("localhost","root","","Mycontacts");
    if(mysqli_connect_error())
    {
       echo "连接失败";
    }
    else
    {
       $sql = "select * from contacts";
    
    $result = $db->query($sql);
    
     echo "<table width=100% cellpadding='0' cellspacing='0' border='1'>";
     echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td>姓名</td><td>电话</td><td>分组</td>";
     while($row=$result->fetch_row())
     {
         $fenzu=GroupsName($db,$row[3]);
         echo "<tr><td><a href='Lshanchu.php?id=".$row[0]."' onclick=\"return confirm('确定删除吗')\">删除</a>&nbsp;&nbsp;<a href='bianji.php?id=".$row[0]."'>编辑</a></td><td>{$row[1]}</td><td>{$row[2]}</td><td>{$fenzu}</td>";
         
     }
    }
    
    
      echo "</table>";
      //根据代号查询分组名称
   function GroupsName($db,$id)
   {
     //写sql语句
     $sql = "select * from groups where id='{$id}'"; 
     //执行sql语句
     $result = $db->query($sql);
     //处理数据
     if($row=$result->fetch_row())
     {
         return $row[1];
     }
     else
     {
        return "";
     }
   }

?>
<form action="tianjia.php" method="post">
<input type="submit" value="添加新号码" onclick="window.open('tianjia.php')"/>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>添加</title>
</head>

<body>
<?php
    $db =new mysqli("localhost","root","","Mycontacts");
    if(mysqli_connect_error())
    {
       echo "连接失败";
    }
    else
    {
       $sql = "select * from contacts";
    
    $result = $db->query($sql);
    
     echo "<table width=100% cellpadding='0' cellspacing='0' border='1'>";
     echo "<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;</td><td>姓名</td><td>电话</td><td>分组</td>";
     while($row=$result->fetch_row())
     {
         $fenzu=GroupsName($db,$row[3]);
         echo "<tr><td><a href='Lshanchu.php?id=".$row[0]."' onclick=\"return confirm('确定删除吗')\">删除</a>&nbsp;&nbsp;<a href='bianji.php?id=".$row[0]."'>编辑</a></td><td>{$row[1]}</td><td>{$row[2]}</td><td>{$fenzu}</td>";
         
     }
    }
    
    
      echo "</table>";
      function GroupsName($db,$id)
   {
     //写sql语句
     $sql = "select * from groups where id='{$id}'"; 
     //执行sql语句
     $result = $db->query($sql);
     //处理数据
     if($row=$result->fetch_row())
     {
         return $row[1];
     }
     else
     {
        return "";
     }
   }

?>
<form action="TJchuli.php" method="post">
<div><span>姓名:</span><input type="text" name="name" /></div>
<div><span>电话:</span><input type="text" name="tel" /></div>
<div><span style="width:90px;">分组:</span>
<select name="groupid">
    <?php 
   $db=new mysqli("localhost","root","","Mycontacts");
   if(mysqli_connect_error())
   {
       echo "连接失败";
   }
   else
   {
     $sql = "select * from groups"; 
     $result = $db->query($sql);
     while($row=$result->fetch_row())
     {
         echo "<option value='{$row[0]}'>{$row[1]}</option>";
     }
   }
    
?>
</select>
</div> 
<div><input type="submit" value="添加" /><a href="LianXiren.php">返回</a></div>

</form>

</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>添加处理</title>
</head>

<body>
<?php 
    $id = $_POST["id"];
    $name = $_POST["name"];
    $tel = $_POST["tel"];
    $groups = $_POST["groupid"];
   $db=new mysqli("localhost","root","","Mycontacts");
   if(mysqli_connect_error())
   {
       echo "连接失败";
   }
   else
   {
     $sql = "insert into contacts values('{$id}','{$name}',{$tel},'{$groups}')"; 
     $result = $db->query($sql);
     if($result)
     {
         header("Location:tianjia.php"); 
     }
     else
     {
         echo "添加失败";
     }
   }

?>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>删除</title>
</head>

<body>
<?php 
    $id = $_GET["id"]; 
    //1.造连接对象
   $db=new mysqli("localhost","root","","Mycontacts");
   //2.判断是否连接成功
   if(mysqli_connect_error())
   {
       echo "连接失败";
       exit; //退出整个程序 
   }
   else
   {
     //3.写sql语句
     $sql = "delete from contacts where id='".$id."'"; 
     //4. 执行sql语句
     $result = $db->query($sql);
     //判断是否删除成功
      if($result)
      {
          header("Location:LianXiren.php");
      }
      else
      {
          echo "删除失败!";
      }
   }
?>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>编辑</title>
</head>

<body>
<?php
 $id = $_GET["id"];
     //1.造连接对象
   $db=new mysqli("localhost","root","","Mycontacts");
   //2.判断是否连接成功
   if(mysqli_connect_error())
   {
       echo "连接失败";
       exit; //退出整个程序 
   }
   else
   {
     //3.写sql语句
     $sql = "select * from contacts where id='".$id."'"; 
     //4. 执行sql语句
     $result = $db->query($sql);
     //5.处理数据
     //遍历每一条数据
     $row=$result->fetch_row();
   }

?>
<form action="bianjichuli.php" method="post">
<div style="text-align:center">编辑联系人信息</div>
<div style="visibility:hidden"><span>id:</span><input type="text"  name="id" value="<?php echo $row[0] ?>" /></div>
<div><span>姓名:</span><input type="text" name="name" value="<?php echo $row[1] ?>" /></div>
<div><span>电话:</span><input type="text" name="tel" value="<?php echo $row[2] ?>" /></div>
<div><span style="width:90px;">分组:</span>
<select name="groupid">
    <?php 
   $db=new mysqli("localhost","root","","Mycontacts");
   if(mysqli_connect_error())
   {
       echo "连接失败";
   }
   else
   {
     $sql = "select * from groups"; 
     $result = $db->query($sql);
     while($row=$result->fetch_row())
     {
         echo "<option value='{$row[0]}'>{$row[1]}</option>";
     }
   }
    
?>
</select>
</div> 
<div><input type="submit" value="修改" /></div>

</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>编辑处理</title>
</head>

<body>
<?php 
    $id = $_POST["id"];
    $name = $_POST["name"];
    $tel = $_POST["tel"];
    $groups = $_POST["groupid"];
   $db=new mysqli("localhost","root","","Mycontacts");
   if(mysqli_connect_error())
   {
       echo "连接失败";
   }
   else
   {
     $sql="update contacts set name='".$name."',tel='".$tel."',groupid='".$groups."' where id='".$id."'";
     $result=$db->query($sql);
    if($result)
    {
        header("Location:tianjia.php");
        }
        else 
        {
        echo "修改失败";
        }
    }
?>
</body>
</html>

实现效果如下图:

posted @ 2016-02-21 19:48  秦萧不再  阅读(291)  评论(0编辑  收藏  举报