dscpg80066 2014-12-03 15:24
浏览 40
已采纳

yii从$ _GET加载数据库中的数据

I have a controller that has a display data from a database depending on the ?id=, it works correctly. However, if you do not give any value id gets error

error 400 Your request is invalid.

My code:

    public function actionIndex($id)
        {
            // renders the view file 'protected/views/site/index.php'
            // using the default layout 'protected/views/layouts/main.php'

           $this->pageTitle = 'Page';

                $criteria = new CDbCriteria(
                    array(
                        'condition' => 'name = :Name',
                        'params' => array(':Name' => $id),
//if $id is not defined then error

                    )
                );
            }
            $ModelPages = Pages::model()->findAll($criteria);
            $this->render('index',
                    array(
                        'Model' => $ModelPages,
                    )
                );
        }

I tried this out in such a way, but it did not help.

public function actionIndex($id)
    {
        // renders the view file 'protected/views/site/index.php'
        // using the default layout 'protected/views/layouts/main.php'

       $this->pageTitle = 'Page';

       if(empty($id)){
           $criteria = new CDbCriteria(
               array(
                   'condition' => 'name = :Name',
                   'params' => array(':Name' => 'index'),
               )
           );
       }
        else {

            $criteria = new CDbCriteria(
                array(
                    'condition' => 'name = :Name',
                    'params' => array(':Name' => $id),
                )
            );


        }
        $ModelPages = Pages::model()->findAll($criteria);
        $this->render('index',
                array(
                    'Model' => $ModelPages,
                )
            );

    }

Is my solution is correct (safe) when it comes to displaying the content according to the site?

  • 写回答

2条回答 默认 最新

  • dry69034 2014-12-03 15:39
    关注

    You solution is correct but better use getQuery() method for fetching GET parameters and handle the error if no pages found:

    public function actionIndex($id='index') //Notice the default parameter value
    {
       $id = Yii::app()->request->getQuery('id', 'index') //if id GET parameter does not exist $id will be 'index'
       $criteria = new CDbCriteria(
            array(
                'condition' => 'name = :Name',
                'params' => array(':Name' => $id),
            )
       );
       $ModelPages = Pages::model()->findAll($criteria);
    
       if (empty($ModelPages)) {
           throw new CHttpExeption(404,'page not found');
       }
    
       $this->render('index',
            array(
                'Model' => $ModelPages,
            )
       );
    }
    

    Also if your action can not receive id parameter you should set default value for it (actionIndex($id='index'))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?