Heim > php教程 > php手册 > Hauptteil

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

WBOY
Freigeben: 2016-06-21 09:05:11
Original
1264 Leute haben es durchsucht

数据|数据库|源代码

//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,'wangyou000@263.net');
INSERT INTO myuser VALUES (2,'金风','武汉','430071','武汉市武昌区水果湖',111,0,0,'jinfeng000@263.net');
INSERT INTO myuser VALUES (3,'常丽','襄樊','441021','襄樊市襄城区南街',111,0,0,'changli000@263.net'); 

1.查寻全部

                                                             
                                                                   
 

                                                            
  查寻全部                                      
                                                            
                                                                   
                                                             
                                                                   
 

                                                
  下面是用户数据库的所有内容:                                     
 

                                                            
                                                                   
                                                                 
   $db = MySQL_connect("localhost", "root");                       
   MySQL_select_db("mysite",$db);                                  
   $result = MySQL_query("SELECT * FROM user",$db);              
                                                                   
   echo "\n";                                      
   echo "";                            
   echo "";                              
   echo "";                          
   echo "";                          
   echo "\n";                                                 
                                                                   
while ($myrow = MySQL_fetch_row($result))                          
   {                                                               
      printf("", $myrow[0], $myrow[1]);
     printf("", $myrow[2], $myrow[3]);       
     printf("", $myrow[4], $myrow[6]);       
     printf("\n", $myrow[7], $myrow[8]);
   }                                                               
echo "
id 姓名城市 邮编住    址 等级权限 邮件地址
%s %s%s %s%s %s%s %s
\n";                                                 
?>                                                                 
                                                                   
                                                            
                      

2.根据id查询

                                                           
                                                                 

                                                           
                                                                 
                                                           <br>     根据id查询                                               <br>                                                         
                                                                 
                                                               
$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("

用户的情况

");                     
   printf("1.ID号 %s\n
", $myrow["id"]);                      
   printf("2.姓名: %s\n
", $myrow["name"]);                   
   printf("3.城市: %s\n
", $myrow["city"]);                   
   printf("4.邮编: %s\n
", $myrow["postcode"]);               
   printf("5.住址: %s\n
", $myrow["address"]);                
   printf("6.等级:%s\n
", $myrow["level"]);                  
   printf("7.邮件地址: %s\n
", $myrow["email"]);              
   printf(" 继续查询 ", $PATH_INFO);          
                                                                 
}                                                                
else                                                             
  {                                                              
                                                                 
   $result = MySQL_query("SELECT * FROM user",$db);            
    if ($myrow = MySQL_fetch_array($result))                     
    {                                                            
      printf("

请选择要查询的用户:

");        
      do {                                                       
        printf("%s%s
\n",           
        $myrow["id"],$PATH_INFO,$myrow["id"], $myrow["name"]);   
          }                                                      
      while ($myrow = MySQL_fetch_array($result));               
    }                                                            
    else                                                         
    {                                                            
      echo "对不起,没有数据!";                              
    }                                                            
  }                                                              
?>                                                               
                                                                 
                                                          
                                                                 
                                                          

3.更新

                                                          
                                                                

                                                          
                                                                
                                                          <br>     更新                                               <br>                                                        
                                                                
                                                                
                                                              
//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("

请选择要修改的用户名字:

");   
      do {                                                      
        printf("%s%s
\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的部分                                             
?>                                                              
   

请对用户的信息进行修改:

                
   
         
        value="">                         
    姓名:">
                                 
    城市:">
                                     
    住址:">
                                  
     电子邮件:">
                                    
            
   
                                                     
                                                              
//PHP 的结束                                                    
65:}                                                       
}                                                               
?>                                                              
                                                         
                                                  

4.增加删除记录

                                                          
                                                                    

                                                              
                                                              <br>   增加删除记录                                                    <br>                                                             
                                                                    
                                                                  
$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 "记录添加成功!

";                                      
   }                                                                
else                                                                
   if ($delete)                                                     
     {                                                              
      // 删除一条记录                                               
      $sql = "DELETE FROM myuser WHERE id=$id";                 
      $result = MySQL_query($sql);                                  
      echo "记录删除成功!

";                                     
     }                                                              
   else                                                             
     {                                                              
    printf("

删除一个用户的所有信息:

");         
    $result = MySQL_query("SELECT * FROM myuser",$db);              
    while ($myrow = MySQL_fetch_array($result))                     
       {                                                            
          printf("%s:%s
\n",
          $myrow["id"],$PATH_INFO, $myrow["id"], $myrow["name"]);   
       }                                                            
  ?>                                                                
                                                                    
 

                                                               
                                                                    
 

                                                   
      添加一个新的用户:                                            
 

                                                             
                                                                    
 
             
 姓名:">
                                     
    城市:">
                                         
    住址:">
                                      
     电子邮件:">
                                        
       
   
                                                         
                                                                    
}                                                                   
?>                                                                  
                                                       
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage