>  기사  >  php教程  >  PHP的数据库增删改查源代码

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

WBOY
WBOY원래의
2016-06-21 09:05:111257검색

数据|数据库|源代码

//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"]);   
       }                                                            
  ?>                                                                
                                                                    
 

                                                               
                                                                    
 

                                                   
      添加一个新的用户:                                            
 

                                                             
                                                                    
 
             
 姓名:">
                                     
    城市:">
                                         
    住址:">
                                      
     电子邮件:">
                                        
       
   
                                                         
                                                                    
}                                                                   
?>                                                                  
                                                       
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.