首页 > 开发 > PHP > 正文

PHP的数据库增删改查源代码

2024-05-04 23:00:17
字体:
来源:转载
供稿:网友

商业源码热门下载www.html.org.cn

//mysql数据库的sql

create table user(  id int(5) default '0' not null auto_increment,  name varchar(20),  city varchar(20),  postcode varchar(10),address varchar(100),  password varchar(10), level tinyint(2) default '0',right tinyint(2),email varchar(40), primary key (id),  unique id (id));
insert into myuser values (1,'王友','北京','100083','北京市海淀区学院路',111,0,0,'[email protected]');
insert into myuser values (2,'金风','武汉','430071','武汉市武昌区水果湖',111,0,0,'[email protected]');
insert into myuser values (3,'常丽','襄樊','441021','襄樊市襄城区南街',111,0,0,'[email protected]'); 

1.查寻全部

<html>                                                             
                                                                   
 <head>                                                            
  <title> 查寻全部</title>                                      
 </head>                                                           
                                                                   
<body>                                                             
                                                                   
  <h2 align=center>                                                
  下面是用户数据库的所有内容:                                     
  </h2>                                                            
                                                                   
<?                                                                 
   $db = mysql_connect("localhost", "root");                       
   mysql_select_db("mysite",$db);                                  
   $result = mysql_query("select * from user",$db);              
                                                                   
   echo "<table border=1>/n";                                      
   echo "<tr><td>id</td><td>姓名</td>";                            
   echo "<td>城市</td><td>邮编</td>";                              
   echo "<td>住    址</td><td>等级</td>";                          
   echo "<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]);
     printf("<td>%s</td><td>%s</td>", $myrow[2], $myrow[3]);       
     printf("<td>%s</td><td>%s</td>", $myrow[4], $myrow[6]);       
     printf("<td>%s</td><td>%s</td></tr>/n", $myrow[7], $myrow[8]);
   }                                                               
echo "</table>/n";                                                 
?>                                                                 
                                                                   
</body>                                                            
</html>                      

2.根据id查询

<html>                                                           
                                                                 
<body>                                                           
                                                                 
  <title>                                                         
     根据id查询                                               
  </title>                                                       
                                                                 
<?                                                               
$db = mysql_connect("localhost", "root");                        
mysql_select_db("mysite",$db);                                   
                                                                 
if ($id) {                                                       
   $result = mysql_query("select * from user where id=$id",$db);
   $myrow = mysql_fetch_array($result);                          
                                                                 
   printf("<h2 align=left>用户的情况</h2>");                     
   printf("1.id号 %s/n<br>", $myrow["id"]);                      
   printf("2.姓名: %s/n<br>", $myrow["name"]);                   
   printf("3.城市: %s/n<br>", $myrow["city"]);                   
   printf("4.邮编: %s/n<br>", $myrow["postcode"]);               
   printf("5.住址: %s/n<br>", $myrow["address"]);                
   printf("6.等级:%s/n<br>", $myrow["level"]);                  
   printf("7.邮件地址: %s/n<br>", $myrow["email"]);              
   printf("<a href=/"%s/"> 继续查询 </a>", $path_info);          
                                                                 
}                                                                
else                                                             
  {                                                              
                                                                 
   $result = mysql_query("select * from user",$db);            
    if ($myrow = mysql_fetch_array($result))                     
    {                                                            
      printf("<h2 align=left>请选择要查询的用户:</h2>");        
      do {                                                       
        printf("%s<a href=/"%s?id=%s/">%s </a><br>/n",           
        $myrow["id"],$path_info,$myrow["id"], $myrow["name"]);   
          }                                                      
      while ($myrow = mysql_fetch_array($result));               
    }                                                            
    else                                                         
    {                                                            
      echo "对不起,没有数据!";                              
    }                                                            
  }                                                              
?>                                                               
                                                                 
</body>                                                          
                                                                 
</html>                                                          

3.更新

<html>                                                          
                                                                
<body>                                                          
                                                                
  <title>                                                        
     更新                                               
  </title>                                                      
                                                                
                                                                
<?                                                              
//php程序的开始                                                 
$db = mysql_connect("localhost", "root");                       
mysql_select_db("mysite",$db);                                  
                                                                
if (!$id)                                                       
  {                                                             
                                                                
   $result = mysql_query("select * from user",$db);           
    if ($myrow = mysql_fetch_array($result))                    
    {                                                           
      printf("<h2 align=left>请选择要修改的用户名字:</h2>");   
      do {                                                      
        printf("%s<a href=/"%s?id=%s/">%s </a><br>/n",          
        $myrow["id"],$path_info,$myrow["id"], $myrow["name"]);  
          }                                                     
      while ($myrow = mysql_fetch_array($result));              
    }                                                           
    else                                                        
    {                                                           
      echo "对不起!数据库中没有记录";                     
    }                                                           
  }                                                             
else                                                            
{                                                               
  if ($submit)                                                  
   {                                                            
    $sql = "update user set name='$name',city='$city',        
    address='$address',email='$email' where id=$id";            
    $result = mysql_query($sql);                                
    echo "谢谢!数据更改完成/n";                                
   }                                                            
  else                                                          
   {                                                            
    $sql = "select * from user where id=$id";                 
    $result = mysql_query($sql);                             
    $myrow = mysql_fetch_array($result);                        
//以下不是php的部分                                             
?>                                                              
    <h2 align=left>请对用户的信息进行修改:</h2>                
    <form method="post" action="<? echo $path_info ?>">         
    <input  type=hidden name="id"                               
    value="<?php echo $myrow["id"] ?>">                         
    姓名:<input type="text" name="first" value="<?php  
   echo $myrow["name"] ?>"><br>                                 
    城市:<input type="text" name="city" value="<?php echo      
    $myrow["city"] ?>"><br>                                     
    住址:<input type="text" name="address" value="<?php echo   
    $myrow["address"] ?>"><br>                                  
     电子邮件:<input type="text" name="email" value="<?php echo
    $myrow["email"] ?>"><br>                                    
     <input type="submit" name="submit" value="修改完毕">       
    </form>                                                     
<?                                                              
//php 的结束                                                    
65:}                                                       
}                                                               
?>                                                              
</body>                                                         
</html>                                                  

4.增加删除记录

<html>                                                          
                                                                    
<body>                                                              
<title>                                                              
   增加删除记录                                                   
</title>                                                            
                                                                    
<?                                                                  
$db = mysql_connect("localhost", "root");                           
mysql_select_db("mysite",$db);                                      
                          &nb, sp;                                         
if ($submit)                                                        
   {                                                                
    $sql = "insert into myuser (name,city,address,email)            
    values ('$name','$city','$address','$email')";                  
    $result = mysql_query($sql);                                    
     echo "记录添加成功!<p>";                                      
   }                                                                
else                                                                
   if ($delete)                                                     
     {                                                              
      // 删除一条记录                                               
      $sql = "delete from myuser where id=$id";                 
      $result = mysql_query($sql);                                  
      echo "记录删除成功!<p>";                                     
     }                                                              
   else                                                             
     {                                                              
    printf("<h2 align=left>删除一个用户的所有信息:</h2>");         
    $result = mysql_query("select * from myuser",$db);              
    while ($myrow = mysql_fetch_array($result))                     
       {                                                            
          printf("%s:<a href=/"%s?id=%s&delete=yes/">%s</a> <br>/n",
          $myrow["id"],$path_info, $myrow["id"], $myrow["name"]);   
       }                                                            
  ?>                                                                
                                                                    
  <p>                                                               
                                                                    
  <h2 align=left>                                                   
      添加一个新的用户:                                            
  </h2>                                                             
                                                                    
  <form method="post" action="<?php echo $path_info?>">             
 姓名:<input type="text" name="name" value="<?php               
   echo $myrow["name"] ?>"><br>                                     
    城市:<input type="text" name="city" value="<?php echo          
    $myrow["city"] ?>"><br>                                         
    住址:<input type="text" name="address" value="<?php echo       
    $myrow["address"] ?>"><br>                                      
     电子邮件:<input type="text" name="email" value="<?php echo    
    $myrow["email"] ?>"><br>                                        
    <input type="submit" name="submit" value="添加用户">   
    </form>                                                         
                                                                    
<?php                                                               
}                                                                   
?>                                                                  
                                                                    
</body>                                                             
                                                                    
</html>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表