php增删改查示例

 
  

创建数据库和用户 CREATE DATABASE mysite; grant all on mysite.* to mysite@localhost identified by '123456'; flush privileges;

create database onbing; grant all on onbing.* to onbing@localhost identified by 'h123TYBcd'; flush privileges;

对于已存在的数据库和用户,使用如下方法: 首先确认数据库(为空)和用户; use mysql select user from user where User='onbing'; update user set password=password('h123TYBcd') where User='onbing'; grant all on onbing.* to onbing@localhost; flush privileges;

在本地hosts文件中添加dns解析 192.168.1.24    www.onbing.com

建表: CREATE TABLE user(id int(5) DEFAULT '1' 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));

CREATE TABLE news(id int(5) DEFAULT '1' NOT NULL ,  title varchar(100),  dates datetime,  contents varchar(200), PRIMARY KEY (id),  UNIQUE id (id)); alter table news modify id integer auto_increment;

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.链接数据库通用方法:conn.php

2.增加 add.php

标题:
内容:

3.删除 del.php

标题:
内容:

 

4.改 edit.php页面


  alert('更新成功'); location.href='select.php'"; echo"更新成功"; } ?> 
标题:
内容:

5.查询页面 select.php

添加内容 

标题: 编辑||删除




  •  

     

     

     

    1.查寻全部

                                                                                                                                                                                                      查寻全部                                                                                                                                                                                                                                                                                                           

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

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

    2.根据id查询

                                                                                                                                                                                                                                                                                                                                根据id查询                                                                                                                                                                           用户的情况");                         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.更新

                                                                                                                                                                                                                                                                                                                           更新                                                                                                                                                                                                                                          请选择要修改的用户名字:");          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的部分                                              ?>                                                                  

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

                        
                  ">                              姓名:">
                                          城市:">
                                              住址:">
                                            电子邮件:">
                                                                                                                                                                                                                                                                                          

    4.增加删除记录

                                                                                                                                                                                                                                                                    增加删除记录                                                                                                                                                                                      ";                                          }                                                                 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"]);           }                                                               ?>                                                                                                                                       

                                                                                                                                          

                                                              添加一个新的用户:                                              

                                                                                                                                        
                   姓名:">
                                              城市:">
                                                  住址:">
                                                电子邮件:">
                                                        
       

    你可能感兴趣的:(php)