douji9518 2015-06-30 07:35
浏览 39
已采纳

使用implode时出现CSV错误

Please help, I'm new in Yii. I want to generate and export CSV file from checked rows in CGridView. When I use static SQL query it works normally, but when I use implode function in WHERE clause - controller returns an error. My button that called controller action:

$this->widget('bootstrap.widgets.TbButtonGroup', array(
'type' => 'primary',
'size'=>'mini',
'buttons' => array(
    array(
        'label' => 'Export',
        'type' => 'success',
        'buttonType'=>'ajaxLink',
        'encodeLabel'=>true,
        'icon'=> 'th white',
        'url'=>Yii::app()->createUrl('/propertyPurchaseSale/ExportChecked'),
        'ajaxOptions'=>array(
        "type" => "post",
        "data" => "js:{ids:$.fn.yiiGridView.getSelection('property-purchase-sale-grid')}",
        "update" => '#', 'success'=>"js:function(data) {window.location.assign('/propertyPurchaseSale/ExportChecked');}"),                                                
                              array( //htmlOptions
                                    )                                    
                                ),
                array(
                    ...
                ),
                ),
            ));

My controller action:

public function actionExportchecked() {
        header('Content-type: text/csv');
        header('Content-type: multipart/form-data');
        header('Content-Disposition: attachment; filename="Export_(' . date('H-i_d.m.Y') .').csv"');
        header('Content-Transfer-Encoding: binary'); 
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Description: File Transfer');
                    $fp = fopen('php://output', 'w');

        if(Yii::app()->request->isAjaxRequest)
        {
            if(isset($_POST['ids']))
            {       
                $idx = $_POST['ids'];
                $count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM property')->queryScalar();
                // $sql='SELECT * FROM property WHERE id  IN (981, 982, 985)';  --> when I use static values - all work
                $sql="SELECT * FROM property WHERE id IN('".implode("', '",$idx)."')";  // --> when I use join or implode function - data exist in firebug, but page return error 500

                $dataProvider=new CSqlDataProvider($sql, array(
                    'totalItemCount'=>$count,
                    'sort'=>array(
                        'attributes'=>array(
                             'fullname', 'address', 'phone', 'db_number', 'created_date'
                        ),
                    ),
                    'pagination'=>false,
                ));
                                    fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
                                    if ($fp)
                                    {                        
                        echo    PropertyPurchaseSale::model()->getAttributeLabel("id").";".
                                    PropertyPurchaseSale::model()->getAttributeLabel("fullname").";".
                                PropertyPurchaseSale::model()->getAttributeLabel("address").";".
                                PropertyPurchaseSale::model()->getAttributeLabel("phone").";".
                                PropertyPurchaseSale::model()->getAttributeLabel("db_number").";".
                                PropertyPurchaseSale::model()->getAttributeLabel("created_date").
                                " 
";                        
                        foreach ($dataProvider->getData() as $data) {
                            echo $data['id'] . '; ' . $data['fullname'] . '; ' . $data['address'] . '; ' . $data['phone'] . '; ' . $data['db_number'] . '; ' . $data['created_date'] . '; ' . "
";
                        }
            }
                    exit;
                }
            }}

Please, help, what am I doing incorrectly?

  • 写回答

1条回答 默认 最新

  • duanmen8491 2015-06-30 08:00
    关注

    My knee-jerk reaction is that there is nothing confirming that $_POST['ids'] is an array. If it is just a string, then implode will fail, the SQL will be improperly formatted, and that would lead to a 500 level error.

    Perhaps this might work:

    // check for empty, that way invalid entry won't go through
    if(!empty($_POST['ids'])) {
        // Check if it is an array
        $input_idx = is_array($_POST['ids'])?
            // if so, then use it as an array
            $_POST['ids']:
            // If not, you need to turn it into an array. I'm only guessing that this should 
            // be a ','. It could be a " " or some other character(s)
            explode(',',$_POST['ids']);
        // Remove all non-numeric items in the array.
        $idx = array_filter($input_idx, 'is_numeric');
        if(!$idx) {
            // Do something with bad data.
        }
        // continue with the line $count = ...
    

    You should also look to making sure that the data is sanitary as those IDs could include SQL injection. (Perhaps this answer)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现