无法用数据库中的字符串填充inputfield

时间:2012-01-10 13:35:43

标签: php zend-framework

更新(最重要的是因为帖子太长了)

好的。新发展。当我添加

resources.db.params.charset = "utf8"
resources.db.params.driver_options.1002 = "SET NAMES utf8;"

到我的bootstrap,标题返回为oriëntatie(它在数据库中为oriëntatie)。但是当我想将oriëntatie添加到我的数据库时,它会一直被剥离到ori

原帖

我已经建立了一个CMS。与任何其他CMS一样,您可以添加/更新页面。

现在,当我添加一个包含例如字符ë的标题的页面时,它会在我的标题字段中放入数据库中。 (我在表单上使用StringTrim和StripTags过滤器。)

现在,当我想更新页面并预填标题输入字段时,我想再次显示ë字符。相反,我的输入字段仍为空。

我试图以所有可能的方式对值(htmletities,html_entity_decode)进行编码和解码,但我只能在表单字段中显示htmlentity值。

我的胆量告诉我这不是正确的方法,但我仍然希望人们在没有拼写错误的情况下添加适当的标题......

建议,提示任何事情都会非常感激!

编辑:添加了一些代码,不确定哪些部分

以下代码会发生这种情况:

在输入字段中添加单词oriëntatie会将oriëntatie放入数据库中。 尝试在更新页面上再次在输入字段中加载值oriëntatie时,输入字段保持为空。我现在确定检索了所有数据。

下面是填充数据库行的屏幕截图。

enter image description here

的application.ini

resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.profiler = true

自举

// Build the view and layouts
protected function _initBuildBase()
{
    $this->bootstrap('view');
    $this->bootstrap('layout');     
    $layout = $this->getResource('layout');
    $this->view = $layout->getView();

    $this->view->doctype("HTML4_STRICT");
    $this->view->setEncoding('UTF-8');
    $this->view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
    $this->view->headMeta()->appendHttpEquiv('Content-Language', 'nl-NL');
    $this->view->headMeta()->appendHttpEquiv('Cache-control', 'public');
    $this->view->headMeta()->appendName('author', 'De Graaf & Partners Communications');
}

update.phtml页面的标题

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>

        <link href="/server_management/domains/cms_version_2/../../_application/public/images/admin/favicon.ico" rel="icon" type="image/x-icon" /><meta http-equiv="Content-Type" content="text/html;charset=utf-8" >

<meta http-equiv="Content-Language" content="nl-NL" >

<meta http-equiv="Cache-control" content="public" >

<meta name="author" content="De Graaf &amp; Partners Communications" >

<meta name="robots" content="noindex, nofollow" ><link href="/server_management/domains/cms_version_2/../../_application/public/css/admin/style.css" media="screen" rel="stylesheet" type="text/css" >

<!--[if IE]> <link href="/server_management/domains/cms_version_2/../../_application/public/css/admin/ie/style.css" media="screen, projection" rel="stylesheet" type="text/css" ><![endif]-->

<!--[if IE]> <link href="/server_management/domains/cms_version_2/../../_application/public/css/blueprint/ie.css" media="screen, projection" rel="stylesheet" type="text/css" ><![endif]-->

<link href="/server_management/domains/cms_version_2/../../_application/public/css/admin/print.css" media="print" rel="stylesheet" type="text/css" ><script type="text/javascript" src="/server_management/domains/cms_version_2/../../_application/public/jquery/jquery.lib.js"></script>

<script type="text/javascript" src="/server_management/domains/cms_version_2/../../_application/public/jquery/jquery.loader.js"></script>

<script type="text/javascript" src="/server_management/domains/cms_version_2/../../_application/public/jquery/jquery.init.js"></script>

<script type="text/javascript" src="/server_management/domains/cms_version_2/../../_application/public/jquery/tinymce/jquery.tinymce.js"></script><title>Pages - Admin - DGPCMS</title> </head>

    <body>

数据库

enter image description here

数据库表

enter image description here

PagesService(插入和更新)

public function InsertPages($url, $parent_page, $title, $text, $keywords, $description, $user, $dashboardmessage)
    {
        $data = array(
            'url' => $url,
            'parent_page' => $parent_page,
            'secure' => 'n',
            'title' => $title,
            'text' => $text,
            'keywords' => $keywords,
            'description' => $description,
            'user_created' => $user,
            'user_modified' => $user,
            'date_created' => time(),
            'date_modified' => time()
        );
        return $this->pages->insert($data);
        $this->DashboardService->InsertDashboard('insert', 'pages', $dashboardmessage, $user);
    }

    public function UpdatePages($id, $url, $parent_page, $title, $text, $keywords, $description, $user, $dashboardmessage)
    {
        $data = array(
            'url' => $url,
            'parent_page' => $parent_page,
            'secure' => 'n',
            'title' => $title,
            'text' => $text,
            'keywords' => $keywords,
            'description' => $description,
            'user_modified' => $user,
            'date_modified' => time()
        );
        $this->pages->update($data, $this->CreateWhereClause($id));
        $this->DashboardService->InsertDashboard('update', 'pages', $dashboardmessage, $user);
    }

PagesController(preDispatch,表单设置)

$this->view->form = new Forms_Pages();
$this->view->form->setElementFilters(array('StringTrim', 'StripTags'));
$this->view->standardform = new Forms_StandardButtons();
$this->view->standardform->setElementFilters(array('StringTrim', 'StripTags'));

PagesController(插入和更新)

public function insertAction()
{
    $this->view->pagesDropdown($this->PagesService->GetAllRootPages(), 'url');
    $pass = false;
    $textArray = array();
    foreach($this->PagesService->GetAllPages() as $result)
    {
        $textArray[] = $result->text;
    }
    if($this->getRequest()->isPost())
    {
        if($this->view->form->isValid($this->getRequest()->getPost()))
        {
            if($this->checkexists->isValid($this->view->urlCleaner($this->view->form->getValue('title'))))
            {
                if(preg_match('/(\\[\\[news:overview\\]\\])/is', $this->view->form->getUnfilteredValue('text')))
                {
                    if(preg_grep('/(\\[\\[news:overview\\]\\])/is', $textArray))
                    {
                        $this->_helper->flashMessenger(array('message' => $this->view->translate('The tag [[news:overview]] was already placed on another page. Please remove it before placing it on another page'), 'status' => 'notice'));
                    }
                    else
                    {
                        $pass = true;
                        $this->cache->save($this->view->urlCleaner($this->view->form->getValue('title')), 'module_newsBasepage');
                    }
                }
                else
                {
                    $pass = true;
                }
                if($pass)
                {
                    $this->lastId = $this->PagesService->InsertPages(
                        $this->view->urlCleaner($this->view->form->getValue('title')),
                        $this->view->form->getValue('parent_page'),
                        $this->view->form->getValue('title'),
                        stripslashes($this->view->form->getUnfilteredValue('text')),    
                        $this->view->form->getValue('keywords'),    
                        $this->view->form->getValue('description'),
                        $this->view->user->username,
                        '<strong>'.$this->view->form->getValue('title').'</strong>'
                    );
                    $this->_helper->flashMessenger(array('message' => $this->view->translate('The '.$this->view->subject.' was succesfully saved'), 'status' => 'success'));
                    if($this->getRequest()->getPost('save_finish') != 'Save')
                    {
                        $this->_redirect('/admin/pages/update/'.$this->lastId);
                    }
                    else
                    {
                        $this->_helper->redirectToIndex();
                    }
                }
            }
            else
            {
                $this->_helper->flashMessenger(array('message' => $this->view->translate('This '.$this->view->subject.' already exists'), 'status' => 'notice'));
            }
        }
        else
        {
            $this->_helper->flashMessenger(array('message' => $this->view->translate('Some errors occured'), 'status' => 'error'));
        }
    }
}

public function updateAction()
{
    $this->view->result = $this->PagesService->GetSinglePage($this->_getParam('id'));
    $this->view->form->populate($this->view->result[0]);
    //$this->view->form->populate(array('title' => html_entity_decode($this->view->result[0]['title'])));
    $this->view->pagesDropdown($this->PagesService->GetAllRootPages(), 'url', $this->view->result[0]['title']);
    $pass = false;
    $textArray = array();
    if($this->getRequest()->isPost())
    {
        if($this->view->form->isValid($this->getRequest()->getPost()))
        {
            foreach($this->PagesService->GetAllPages() as $result)
            {
                if($result->id != $this->view->result[0]['id'])
                {
                    $textArray[] = $result->text;
                }
            }
            if($this->view->form->getValue('title') != $this->view->result[0]['title'])
            {
                if($this->checkexists->isValid($this->view->urlCleaner($this->view->form->getValue('title'))))
                {
                    $pass = true;
                }
                else
                {
                    $this->_helper->flashMessenger(array('message' => $this->view->translate('This '.$this->view->subject.' already exists'), 'status' => 'notice'));
                }
            }
            if(preg_match('/(\\[\\[news:overview\\]\\])/is', $this->view->form->getUnfilteredValue('text')))
            {
                if(preg_grep('/(\\[\\[news:overview\\]\\])/is', $textArray))
                {
                    $this->_helper->flashMessenger(array('message' => $this->view->translate('The tag [[news:overview]] was already placed on another page. Please remove it before placing it on another page'), 'status' => 'notice'));
                }
                else
                {
                    $pass = true;
                    $this->cache->save($this->view->urlCleaner($this->view->form->getValue('title')), 'module_newsBasepage');
                }
            }
            else
            {
                $pass = true;
            }
            if($pass == true)
            {
                $this->lastId = $this->PagesService->UpdatePages(
                    $this->_getParam('id'),
                    $this->view->urlCleaner($this->view->form->getValue('title')),
                    $this->view->form->getValue('parent_page'),
                    $this->view->form->getValue('title'),
                    stripslashes($this->view->form->getUnfilteredValue('text')),    
                    $this->view->form->getValue('keywords'),    
                    $this->view->form->getValue('description'),
                    $this->view->user->username,
                    '<strong>'.$this->view->form->getValue('title').'</strong>'
                );
                $this->_helper->flashMessenger(array('message' => $this->view->translate('The '.$this->view->subject.' was succesfully saved'), 'status' => 'success'));
                if(!$this->getRequest()->getPost('save_finish') != 'Save')
                {
                    $this->_helper->redirectToIndex();
                }
                else
                {
                    $this->_redirect('/admin/pages/update/'.$this->_getParam('id'));
                }
            }
        }
        else
        {
            $this->_helper->flashMessenger(array('message' => $this->view->translate('Some errors occured'), 'status' => 'error'));
        }
    }
}

这是Zend_Debug::dump($this->view->result);

的结果
array(1) {
  [0] => array(13) {
    ["id"] => string(3) "188"
    ["order"] => string(1) "0"
    ["url"] => string(10) "orientatie"
    ["parent_page"] => string(3) "n/a"
    ["secure"] => string(1) "n"
    ["title"] => string(10) "oriëntatie"
    ["text"] => string(13) "<p>Test 3</p>"
    ["keywords"] => string(6) "Test 1"
    ["description"] => string(6) "Test 2"
    ["user_created"] => string(5) "Admin"
    ["user_modified"] => string(5) "Admin"
    ["date_created"] => string(10) "1326280122"
    ["date_modified"] => string(10) "1326280122"

这是html输出

<div class="padding_row">
<label for="title" class="required">Title</label>
<input type="text" name="title" id="title" value="" class="form_validator"> <div class="form_validator_box"> <a href="#" title="This page already exists" class="form_validator_result_bad"></a> </div>
</div>

5 个答案:

答案 0 :(得分:1)

我见过这种情况的唯一一次是客户端(浏览器)不知道正确的字符编码。

我看到您已将相应的HTTP-Equiv元数据添加到HeadMeta帮助程序中,但实际上是在视图或布局中显示它吗?

您应该在布局的<head>部分中使用此类内容(如果不使用布局,则应查看)

<head>
    <?php echo $this->headMeta() ?>

我对你的Bootstrap课有点好奇。你能否列出你列出的两条线的其余代码?为什么视图显然是Bootstrap的属性以及它是如何分配的?

答案 1 :(得分:1)

转储显示正确的字符串。所以这不是数据库问题。

但我注意到,你做了

$this->view->form->isValid($this->getRequest()->getPost())

在更新操作中填充()后填充()。 isValid方法包括已填充表单。所以在这里你可以用POST中的空字符串覆盖你的字符串。你应该将你的populate()移动到

if($this->getRequest()->isPost()) {
    //...
}
else{
    // here
}

或删除它并用

替换isValid(...)
$this->view->form->isValid($this->view->result[0]) 

如果错误仍然在其他地方插入

Zend_Debug::dump($this->view->form->getValue('title'));
每次主要操作后

确定您的字符串更改为空白的位置。

答案 2 :(得分:0)

由于HTML页面包含Content-Type text/html;charset=utf-8,并且数据库具有正确的排序规则和字符集,因此问题必须在应用程序部分中。

下一个可能的问题可能是与数据库的连接,但是因为你在mySQL连接上设置了“SET NAMES'utf8'”,所以问题可能出现在下面唯一合乎逻辑的下一步:

$this->view->form->setElementFilters(array('StringTrim', 'StripTags'));

您是否可以尝试从过滤器中删除StripTags,以便测试问题是否存在?

答案 3 :(得分:0)

你是否正在使用这个bug? http://framework.zend.com/issues/browse/ZF-11533

答案 4 :(得分:0)

我找到了解决方案,我可以打败自己,因为我没有更快地想到这一点而完全错过了这一点。

首先是解决方案:

在我的.htaccess文件中,我的规则AddDefaultCharset iso-8859-1当然必须是AddDefaultCharset UTF-8

几天之后,我决定在单独离开几天之后再回顾一下这个问题(尽管这些日子一直困扰着我)。我查找了如何为Zend应用程序设置字符编码。我的所有设置都是正确的,接受我没有将accept-charset设置为我的表单。所以在我这样做之后,我得到了一个回报:orïëntätïës

数据已正确添加到数据库中,所以我想我必须utf-8_decode返回,我是对的。现在我肯定知道某个地方设置了不同的字符集。我在php.net (link)上查看了utf8_decode的说明,这引发了我:

  

utf8_decode - 将使用UTF-8编码的ISO-8859-1字符转换为单字节ISO-8859-1

然后我在我的完整应用程序中搜索了字符串ISO-8859-1,并且搜索返回了这是在我的.htaccess文件中。所以我所要做的就是改变它,一切都很好:)