PHP 之Mysql增删改查操作案例

系统 2018 0

1:user表:

      
        CREATE
      
      
        TABLE
      
       `
      
        user
      
      
        ` (

  `id` 
      
      
        int
      
      (
      
        11
      
      ) 
      
        NOT
      
      
        NULL
      
      
         AUTO_INCREMENT,

  `name` 
      
      
        varchar
      
      (
      
        32
      
      ) 
      
        NOT
      
      
        NULL
      
      
        ,

  
      
      
        PRIMARY
      
      
        KEY
      
      
         (`id`)

) ENGINE
      
      
        =
      
      MyISAM 
      
        DEFAULT
      
       CHARSET
      
        =
      
      utf8
    

2:mysqltools.php  (mysql工具类)

      <?
      
        php

    
      
      
        class
      
      
         MySqlTools{

    
      
      
        private
      
      
        $host
      
      ='127.0.0.1'
      
        ;

    
      
      
        private
      
      
        $uname
      
      ='root'
      
        ;

    
      
      
        private
      
      
        $pwd
      
      ='mysql'
      
        ;

    
      
      
        private
      
      
        $dbname
      
      ='test'
      
        ;

    

    
      
      
        private
      
      
        $conn
      
      
        ;

    

    
      
      
        function
      
      
         __construct(){

        
      
      
        $this
      
      ->conn=
      
        mysql_connect
      
      (
      
        $this
      
      ->host,
      
        $this
      
      ->uname,
      
        $this
      
      ->
      
        pwd)

        or 
      
      
        die
      
      ('mysql_connect error:'.
      
        mysql_error
      
      
        ());

        
      
      
        mysql_select_db
      
      (
      
        $this
      
      ->
      
        dbname)

        or 
      
      
        die
      
      ('mysql_select_db error:'.
      
        mysql_error
      
      
        ());

        
      
      
        mysql_query
      
      ("set names 'utf8'"
      
        );    

    }    



    
      
      
        function
      
       exec_dql(
      
        $sql
      
      
        ){

        
      
      
        $result
      
      =
      
        mysql_query
      
      (
      
        $sql
      
      ,
      
        $this
      
      ->
      
        conn);

        
      
      
        $arr
      
      =
      
        array
      
      
        ();

        
      
      
        while
      
      (
      
        $row
      
      =
      
        mysql_fetch_assoc
      
      (
      
        $result
      
      
        )){

        
      
      
        $arr
      
      []=
      
        $row
      
      
        ;

        }

        
      
      
        mysql_free_result
      
      (
      
        $result
      
      
        );

        
      
      
        return
      
      
        $arr
      
      
        ;

    }



    
      
      
        function
      
       exec_dml(
      
        $sql
      
      
        ){

        
      
      
        return
      
      
        mysql_query
      
      (
      
        $sql
      
      ,
      
        $this
      
      ->
      
        conn);    

    }



    
      
      
        function
      
      
         free(){

        
      
      
        mysql_close
      
      (
      
        $this
      
      ->
      
        conn);    

    }

    }


      
      ?>
    

3:index.php  (首页)

PHP 之Mysql增删改查操作案例

      <html>

    <head>

    <title>Index</title>

    </head>

    <body>

    <?
      
        php

        
      
      
        require_once
      
       'mysqltools.php'
      
        ;

        
      
      
        $mysql
      
      =
      
        new
      
      
         MySqlTools();

        
      
      
        $sql
      
      ='select id,name from user'
      
        ;

        
      
      
        $users
      
      =
      
        $mysql
      
      ->exec_dql(
      
        $sql
      
      
        );    

    
      
      ?>

    <table style="width:50%;">

        <tr>

        <th>ID</th>

        <th>Name</th>

        <th>Oper</th>

        </tr>

        <?
      
        php

        
      
      
        foreach
      
      (
      
        $users
      
      
        as
      
      
        $user
      
      
        ){

        
      
      ?>

        <tr align="center">

        <td><?php 
      
        echo
      
      
        $user
      
      ['id'];?></td>

        <td><?php 
      
        echo
      
      
        $user
      
      ['name'];?></td>

        <td>

            <a href="show.php?id=<?php echo 
      
        $user
      
      ['id'];?>">详情</a>

            <a href="delete.php?id=<?php echo 
      
        $user
      
      ['id'];?>">删除</a>

        </td>

        </tr>

        <?php }?>

    </table>

    <hr/>

    <h2><a href="add.php">Add a 
      
        new
      
       user</a></h2>

    </body>

</html>
    

4:add.php  (增加新用户)

PHP 之Mysql增删改查操作案例

      <html>

    <head>

    <title>Add</title>

    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>

    </head>



    <body>

    <h1>Add</h1><a href="index.php">Index</a><br/>

    <?
      
        php

        
      
      
        if
      
      (!
      
        empty
      
      (
      
        $_POST
      
      ['uname'
      
        ])){

        
      
      
        $name
      
      =
      
        $_POST
      
      ['uname'
      
        ];

        
      
      
        require_once
      
       'mysqltools.php'
      
        ;

        
      
      
        $sql
      
      ="insert into user(name) values('
      
        $name
      
      ')"
      
        ;    

        
      
      
        $mysql
      
      =
      
        new
      
      
         MySqlTools();

        
      
      
        if
      
      (
      
        $mysql
      
      ->exec_dml(
      
        $sql
      
      
        )){

            
      
      
        echo
      
       '<h3>Add Success!</h3>'
      
        ;        

        }
      
      
        else
      
      
        {

            
      
      
        echo
      
       '<h3>Add Error!</h3>'
      
        ;

        }

        }

    
      
      ?>

    <form method="post">
      
        

        Name
      
      :<input type="text" name="uname"/>

        <input type="submit" value="Add"/>

    </form>

    </body>

</html>
    

5:delete.php  (删除操作)

      <?
      
        php

    
      
      
        $id
      
      =
      
        $_GET
      
      ['id'
      
        ];



    
      
      
        if
      
      (
      
        isset
      
      (
      
        $id
      
      
        )){

    
      
      
        require_once
      
       'mysqltools.php'
      
        ;

    
      
      
        $mysql
      
      =
      
        new
      
      
         MySqlTools();

    

    
      
      
        $sql
      
      ="delete from user where id=
      
        $id
      
      "
      
        ;

    
      
      
        $mysql
      
      ->exec_dml(
      
        $sql
      
      
        );

    }



    
      
      
        header
      
      ('Location: index.php'
      
        );


      
      ?>
    

6:show.php  (显示详细信息页面)

PHP 之Mysql增删改查操作案例

      <?
      
        php

    
      
      
        header
      
      ('Conent-Type:text/html;charset=utf-8'
      
        );



    
      
      
        $id
      
      =
      
        $_GET
      
      ['id'
      
        ];

    
      
      
        $name
      
      =''
      
        ;

    
      
      
        if
      
      (
      
        isset
      
      (
      
        $id
      
      
        )){

    
      
      
        require_once
      
       'mysqltools.php'
      
        ;

    
      
      
        $mysql
      
      =
      
        new
      
      
         MySqlTools();

    
      
      
        $sql
      
      ="select name from user where id=
      
        $id
      
       limit 1"
      
        ;

    
      
      
        $arr
      
      =
      
        $mysql
      
      ->exec_dql(
      
        $sql
      
      
        );    

    
      
      
        $name
      
      =
      
        $arr
      
      [0]['name'
      
        ];

    }



    
      
      
        echo
      
       "ID:
      
        $id
      
      <br/>Name:
      
        $name
      
      <br/>"
      
        ;

    
      
      
        echo
      
       '<h3><a href="index.php">Index</a></h3>'
      
        ;


      
      ?>
    

 

 

PHP 之Mysql增删改查操作案例


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论