在数据库中插入值,zend框架无法正常工作

时间:2011-07-11 06:45:44

标签: mysql zend-framework pdo

控制器:

<?php
class AdminController extends Zend_Controller_Action
{
    public function init()
    {
        error_reporting(E_ALL);
    }

    public function indexAction()
    {
        // blank //
    }

    public function registerAction()
    {
        // database configuration //
        $config = array(
            'host' => 'localhost',
            'username' => 'root',
            'password' => '',
            'dbname' => 'zendfirst'
        );
        $db = new Zend_Db_Adapter_Pdo_Mysql($config);

        // get request(get/post) //
        $request = $this->getRequest();     

        if($request->isPost('fsubmit')) {
            $insert_data = array(
                'first_name' => $request->first_name,
                'last_name' => $request->last_name
            );
            $db->insert('admin', $insert_data);
        }
    }
}

查看:

<form method="post" action="">

    <div class="form_div">
        <label for="first_name">First Name</label>
        <input type="text" value="" name="first_name" id="first_name" />    
    </div>

    <div class="form_div">
        <label for="last_name">Last Name</label>
        <input type="text" value="" name="last_name" id="last_name" />
    </div>

    <div class="form_div">
        <input type="submit" value="Register" name="fsubmit" id="fsubmit" />
    </div>

</form>

<style>
.form_div {
    padding : 5px;
}
</style>

设置“php.ini”

extension=php_pdo_mysql.dll

数据库表:

admin_id  first_name  last_name
--------  ----------  ---------

抛出错误

An error occurred
Application error

有什么问题?在Zend Framework中是否有类似CodeIgniter的“探查器”?

1 个答案:

答案 0 :(得分:0)

您将post值视为对象。试试这个:

    public function registerAction()
    {
        // database configuration //
        $config = array(
            'host' => 'localhost',
            'username' => 'root',
            'password' => '',
            'dbname' => 'zendfirst'
        );
        $db = new Zend_Db_Adapter_Pdo_Mysql($config);

        // get request(get/post) //
        $request = $this->getRequest();     

        if($request->isPost('fsubmit')) {
            $post_data = $request->getPost();
            $insert_data = array(
                'first_name' => $post_data['first_name'],
                'last_name' => $post_data['last_name']
            );
            $db->insert('admin', $insert_data);
        }
    }