原生代码封装好的增删改查

封装好增删改查的sql语句

1.建立一个DB.class.php

在CODE上查看代码片派生到我的代码片

    <?php  
      //解析头  
      header('content-type:text/html;charset=utf-8');  
      class DB{  
         function __construct($host,$user,$pass,$dbname){  
           $link=mysql_connect($host,$user,$pass);  
           mysql_select_db($dbname,$link);  
           mysql_query('set names utf8');  
         }  
         //查询sql  
         function select($table,$table1,$id){  
           //准备查询的sql语句  
           $sql="select * from $table inner join $table1 on $table.$id=$table1.$id";  
           //执行  
           $res=mysql_query($sql);  
           //定义空数组  
           $arr=array();  
           while($row=mysql_fetch_assoc($res)){  
             $arr[]=$row;  
           }  
           return $arr;  
         }  
         function all($table){  
          $sql="select * from  $table";  
          //echo $sql;die;  
          $res=mysql_query($sql);  
          //var_dump($res);die;  
          $arr=array();  
          while($row=mysql_fetch_assoc($res)){  
            $arr[]=$row;  
          }  
          //var_dump($arr);die;  
          return $arr;  
        }  
         //添加sql语句  
         function add($table,$arr){  
           $str=array_values($arr);  
           $str=implode("','",$str);  
           $sql="insert into $table values(null,'$str')";  
           //echo $sql;die;  
           $res=mysql_query($sql);  
           if($res&&mysql_affected_rows()>0){  
             return true;  
           }else{  
             return false;  
           }  
         }  
         //删出sql语句  
         function del($table,$where){  
           $sql="delete from $table where $where";  
           $res=mysql_query($sql);  
           if($res&&mysql_affected_rows()>0){  
             return true;  
           }else{  
             return false;  
           }  
         }  
         //查询修改语句需要查询的语句  
         function update($table,$where){  
           $sql="select * from $table where $where";  
           //echo $sql;die;  
           $res=mysql_query($sql);  
           $row=mysql_fetch_assoc($res);  
           //var_dump($row);die;  
           return $row;  
         }  
         //改  
         function updateok($table,$update,$where){  
           $sql="update $table set $update where $where";  
           $res=mysql_query($sql);  
           if($res&&mysql_affected_rows()>0){  
             return true;  
           }else{  
             return false;  
           }  
         }  
      }  
    ?>  


1.form表单页面form.php

在CODE上查看代码片派生到我的代码片

    <?php  
      include 'DB.class.php';  
      $db=new DB('127.0.0.1:3306','root','root','dongwu');  
      $arr=$db->all('nickname');  
      //var_dump($arr);die;  
    ?>  
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">  
    <head>  
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />  
        <title></title>  
    </head>  
    <body>  
        <center>  
          <form action="add.php" method='post'>  
            <table border=1>  
              <tr>  
                <td>请选择昵称:</td>  
                <td>  
                 <select name="nid">  
                   <?php foreach($arr as $v){?>  
                     <option value="<?php echo $v['nid'];?>"><?php echo $v['name'];?></option>  
                   <?php }?>  
                 </select>  
                </td>  
              </tr>  
              <tr>  
                <td>留言内容:</td>  
                <td><textarea name="content" cols="20" rows="5"></textarea></td>  
              </tr>  
              <tr>  
                <td><input type="submit" value='提交留言'/></td>  
                <td></td>  
              </tr>  
            </table>  
          </form>  
        </center>  
    </body>  
    </html>  



2.添加sql语句,直接引进DB.class.php

add.php

在CODE上查看代码片派生到我的代码片

    <?php  
      include 'DB.class.php';  
      $db=new DB('127.0.0.1:3306','root','root','dongwu');  
      $arr=$_POST;  
      $res=$db->add('message',$arr);  
      if($res){  
        echo "<script>alert('添加成功');location.href='list.php'</script>";  
      }else{  
        echo "<script>alert('添加失败');location.href='form.php'</script>";  
      }  
    ?>  


3.删除sql语句,直接引进DB.class.php

delete.php

在CODE上查看代码片派生到我的代码片

    <?php  
      include 'DB.class.php';  
      $db=new DB('127.0.0.1:3306','root','root','dongwu');  
      $id=$_GET['id'];  
      $res=$db->del('message',"mid=$id");  
      if($res){  
         echo "<script>alert('删除成功');location.href='list.php'</script>";  
      }else{  
        echo "<script>alert('删除失败');location.href='list.php'</script>";  
      }  
    ?>  


4.显示表单list.php

在CODE上查看代码片派生到我的代码片

    <?php  
      include 'DB.class.php';  
      $db=new DB('127.0.0.1:3306','root','root','dongwu');  
      $arr=$db->select('message','nickname','nid');  
    ?>  
    <center>  
      <table border=1>  
        <tr>  
            <th>昵称</th>  
            <th>留言内容</th>  
            <th>操作</th>  
        </tr>  
        <?php foreach($arr as $v){?>  
          <tr>  
            <td><?php echo $v['name'];?></td>  
            <td><?php echo $v['content'];?></td>  
            <td><a href="delete.php?id=<?php echo $v['mid'];?>">删除</a>||<a href="update.php?id=<?php echo $v['mid'];?>">修改</a></td>  
          </tr>  
        <?php }?>  
      </table>  
    </center>  


5.修改数据

在CODE上查看代码片派生到我的代码片

    <?php  
      include 'DB.class.php';  
       $db=new DB('127.0.0.1:3306','root','root','dongwu');  
      $id=$_GET['id'];  
      $row=$db->update('message',"mid=$id");  
      //var_dump($row);die;  
      $arr=$db->all('nickname');  
    ?>  
    <center>  
      <form action="updateok.php?id=<?php echo $row['mid'];?>" method='post'>  
        <table border=1>  
          <tr>  
            <td>请选择昵称:</td>  
            <td>  
             <select name="nid">  
               <?php foreach($arr as $v){?>  
                 <option value="<?php echo $v['nid'];?>" <?php if($row['nid']=$v['nid']){ echo 'selected';}?>><?php echo $v['name'];?></option>  
               <?php }?>  
             </select>  
            </td>  
          </tr>  
          <tr>  
            <td>留言内容:</td>  
            <td><textarea name="content" cols="20" rows="5"><?php echo $row['content'];?></textarea></td>  
          </tr>  
          <tr>  
            <td><input type="submit" value='修改留言'/></td>  
            <td></td>  
          </tr>  
        </table>  
      </form>  
    </center>  


将修改的数据调到updateok.php中

在CODE上查看代码片派生到我的代码片

    <?php  
      include 'DB.class.php';  
      $nid=$_POST['nid'];  
      $content=$_POST['content'];  
      $id=$_GET['id'];  
      $db=new DB('127.0.0.1:3306','root','root','dongwu');  
      $res=$db->updateok('message',"content='$content',nid=$nid","mid=$id");  
      if($res){  
        echo "<script>alert('修改成功');location.href='list.php'</script>";  
      }else{  
        echo "<script>alert('修改失败');location.href='list.php'</script>";  
      }  
    ?>  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值