php - 无法用数据库中的字符串填充输入字段

标签 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

应用程序.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>

最佳答案

我唯一一次看到这种情况是在客户端(浏览器)不知道正确的字符编码时发生的。

我看到您已将适当的 HTTP-Equiv 元添加到 HeadMeta 中助手,但您实际上是在 View 或布局中显示它吗?

你应该在 <head> 中有这样的东西您的布局部分(或 View ,如果不使用布局)

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

我对你的Bootstrap有点好奇类(class)。你能展示你列出的两行周围的其余代码吗?为什么 View 显然是 Bootstrap 的属性它是如何分配的?

关于php - 无法用数据库中的字符串填充输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8804101/

相关文章:

php - 在 Magento 1.9.1.1 中重新索引类别产品时出现问题

zend-framework - Zend Framework 中的 renderViewHelper 做了什么?

php - 如何添加 View 助手目录(zend 框架)

zend-framework - 使用 jQuery Form Plugin 解析 JSON 对象时出错

php - mysql 表中没有重复值,但某些回显值重复

php - 在php中从Mysql获取当前用户ID

php - json, php - 从数组输出字符串

php - 使用 Zend_Http_Client 的客户端 ssl 证书

php - 数据表自动获取气泡编辑器(Laravel + Blades)的ad值

php - 通过在组合框中选择来过滤表