您的位置:首页 > 数据库

数据库字段修改

2017-11-23 11:48 148 查看
public function actionField_upd()

    {

        $name=$_GET['name'];

        $id=$_GET['id'];

        // var_dump($id);die;

        //字段

        $info=Yii::$app->db->createCommand("show FULL fields from `$name`")->queryAll();

        //查询出这条ID的内容

        $res=Yii::$app->db->createCommand("SELECT * FROM `$name` WHERE `id`=$id")->queryOne();

        //循环出字段

        foreach($info as $k=>$v)

        {

            $bb[]=$v['Field'];

        }

        //删除ID

        // unset($bb[0]);

        //var_dump($aa);die;

        $cl=current($bb);

        unset($bb[$cl]);

        // var_dump($res);die;

        return $this->render('field_upd',['info'=>$bb,'res'=>$res,'name'=>$name]);
    }

//html页面

<meta charset="utf8">

<style>

table{ border-collapse: collapse; border: 1px solid #ddd; width: 800px; margin: 0 auto;margin-top: 50px; background: rgba(121, 217, 221, 0.4); color: #666}

table tr{ height: 40px;}

table td{ border: 1px solid #ddd; text-align: center}

*{margin: 0; padding:0 ; font-family: 微软雅黑}

a{ text-decoration: none; color: #666;}

.top{ width: 100%; background: rgba(14, 196, 210, 0.99); color: #fff; height: 100px; line-height: 150px; text-align: right;}

.top span{ margin-right: 20px}

.left{ width: 260px; float: left; height: 100%; background: rgba(121, 217, 221, 0.4)}

.left ul{ list-style: none; width: 100%;}

.left ul li{ height: 40px; width: 100%; border: 1px solid #ddd; line-height: 40px; text-align: center;}

.left .selected{ background: rgba(14, 196, 210, 0.99);}

.left .selected a{ color: #fff;}

.right{ float: left; width: 1000px;}

.title{ background: rgba(14, 196, 210, 0.99); color: #fff}

.search-box{ width: 900px; margin: 0 auto; margin-top: 100px; }

</style>

<div class="top">

    <span>欢迎管理员:admin</span>

</div>

<div class="left">

    <ul>

        <li class="selected"><a href="#">添加表字段</a></li>

    </ul>

</div>

<div class="right">

    <div class="sea
4000
rch-box">

        <form action="?r=secondweek/field_upds" method="post">

            <table>

                <?php foreach($info as $k=>$v) {?>

                <td><?php echo $v?></td>

                <?php } ?>

                <tr>

                <?php foreach($res as $k=>$value) { ?>

                <td><input type="text" name="<?php echo $value?>" value="<?php echo $value?>"></td>

                <?php } ?>

                </tr>

                <td><input type="submit" value="修改"></td>

            </table>

            <td><input type="hidden" name="name" value="<?php echo $name?>"></td>

    </form>

    </div>

</div>

处理修改

    public function actionField_upds()

    {

        $post=$_POST;

        $id=current($post);

        array_pop($post);

        array_shift($post);

        $name=$_POST['name'];

        $del=Yii::$app->db->createCommand("DELETE FROM `yii`.`$name` WHERE `$name`.`id` = $id")->execute();

        // unset(current($post));

        $info=Yii::$app->db->createCommand("show FULL fields from `$name`")->queryAll();

        foreach($info as $k=>$v)

        {

            $bb[]=$v['Field'];

        }    

        unset($bb['0']);

        //var_dump($bb);die;

        //把关联数组转化为索引数组

        foreach($bb as $k=>$v)

        {

            $bb1[] = $v;

        }

        

        foreach($post as $k=>$v)

        {

            $post1[] = $v;

        }

        // print_r($post1);exit;

        // //定义新数组

        $temp_arr = [];

        foreach($bb1 as $k=>$v)

        {

            $temp_arr[$v] = $post1[$k];

        }

        // var_dump($temp_arr);die;
        $add=Yii::$app->db->createCommand()->insert($name,$temp_arr)->execute();

//全选、反选 处理

多选框

  <td><input type="checkbox" name='d' value="<?php echo $v['id']?>"></td>

 <center>

                   <input type="button" value="全选" onclick="check_a()">

    <input type="button" value="反选" onclick="check_b()">

   <td><a href=""></a></td>

        </center>

    </div>

</div>

<script>

   var _all=document.getElementsByName("d");

  function check_a(){

   

    for(var i in _all){

      _all[i].checked=true;

    }

   }

function check_b(){

    for(var i in _all)

        if(_all[i].checked==true)

            {_all[i].checked=false;}

        else

        {

             _all[i].checked=true;

        }

}

</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: