【问题标题】:ErrorException [Notice]:Array to string conversion PHP FormErrorException [注意]:数组到字符串转换 PHP 表单
【发布时间】:2014-01-09 15:41:50
【问题描述】:

我正在创建一个非常简单的表单来从用户那里收集数据并存储在数据库中,但是我收到了一个错误:

ErrorException [注意]:数组到字符串的转换 SYSPATH\classes\kohana\html.php

我之前在我的另一个表单中使用过此代码,我从来没有遇到任何问题,也许我遗漏了什么?

<div id="formWrapper">
 <?php
{
         echo form::open('', array('id'=>'newprefecture'));

         echo form::label('NewPrefecture', 'prefecture:');
         echo form::input('form[NewPrefecture]', array('class'=>'input', 'id'=>'NewPrefecture'));

         echo form::submit('btnSubmit', 'Submit', array('id'=>'btnSubmit', 'class'=>'button'));
         echo form::close();
}
         ?>
         </div>


public function action_newprefecture(){
          $this->template->content = View::factory('admin/main-menu');
          $this->template->content->val = '';
    $this->template->content->post = '';

    if ($this->request->post('form')) {
                                    $post = $this->request->post('form');
    $stmt = DB::query(Database::INSERT, 'INSERT INTO `prefectures` (`prefecture`)
                                                          VALUES (:NewPrefecture)'); 
          $stmt->param(':NewPrefecture', $post['NewPrefecture']);
          try {
                                                $stmt->execute();
                                                $this->template->content->post = $post;
                                                $this->template->content->thanks = true;
                                                } catch (Exception $e) {
                                                    FB::error($e);
                                                }

                    }
                }        

【问题讨论】:

  • 错误应该指向代码中的特定行 - 是它?
  • 1) 这不是一个错误,它是一个通知,它只是意味着您将一个数组传递给一个函数,该函数试图将该数组作为一个字符串echo。 2) 正确缩进/格式化代码 3) 添加通知中的行号 4) 随时随地避免使用静态方法
  • 第 66 行 - 返回 htmlspecialchars( (string) $value, ENT_QUOTES, Kohana::$charset, $double_encode);

标签: php database forms kohana


【解决方案1】:

看起来您可能需要在属性之前的输入中添加默认值(或 NULL),如下所示:

echo form::input('form[NewPrefecture]', NULL, array('class'=>'input', 'id'=>'NewPrefecture'));

http://kohanaframework.org/3.0/guide/api/Form#input

【讨论】:

    猜你喜欢
    • 2013-04-18
    • 1970-01-01
    • 2013-07-02
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 2018-03-13
    • 1970-01-01
    相关资源
    最近更新 更多