如何使用Code Igniter框架将数据插入表中?

时间:2014-11-25 00:10:40

标签: php mysql codeigniter insert

我正在尝试使用代码点火器连接到我的本地计算机上的数据库。我查看运行,但收到​​以下错误:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: client
Filename: views/client.php
Line Number: 1

我收到的第二个错误如下:

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/client.php
Line Number: 1

我去mySQL Workbench检查我的数据库,没有数据输入数据库。我的CORE模型的代码如下:

<?php

class MY_Model extends CI_Model {
const DB_TABLE = 'abstract';
const DB_TABLE_PK = 'abstract';

/**
 * Create record.
 */
private function insert() {
    $this->db->insert($this::DB_TABLE, $this);
    $this->{$this::DB_TABLE_PK} = $this->db->insert_id();
}

/**
 * Update record.
 */
private function update() {
    $this->db->update($this::DB_TABLE, $this, $this::DB_TABLE_PK);
}

/**
 * Populate from an array or standard class.
 * @param mixed $row
 */
public function populate($row) {
    foreach ($row as $key => $value) {
        $this->$key = $value;
    }
}

/**
 * Load from the database.
 * @param int $id
 */
public function load($id) {
    $query = $this->db->get_where($this::DB_TABLE, array(
        $this::DB_TABLE_PK => $id,
    ));
    $this->populate($query->row());
}

/**
 * Delete the current record.
 */
public function delete() {
    $this->db->delete($this::DB_TABLE, array(
        $this->DB_TABLE_PK=>$this->{$this::DB_TABLE_PK}
    ));
    unset($this->{$this::DB_TABLE_PK});
}

/**
 * Save the record.
 */
public function save() {
    if (isset($this->{$this::DB_TABLE_PK})) {
        $this->update();
    }
    else {
        $this->insert();
    }
}

/**
 * Get an array of Models with optional limit, offset.
 * 
 * @ param int $limit Optional.
 * @ param int $offset Optional; if set, requires $limit.
 * @ return array Models populated by database, keyed by PK.
 */
public function get($limit = 0, $offset = 0) {
    if($limit) {
        $query = $this->db->get($this::DB_TABLE, $limit, $offset);
    }
    else {
        $query = $this->db->get($this::DB_TABLE);
    }
    $ret_val = array();
    $class = get_class($this);
    foreach($query->result as $row) {
        $model = new $class;
        $model->populate($row);
    $ret_val[$row->{$this::DB_TABLE_PK}] = $model;
    }
    return $ret_val;
}
}

我的客户端模型如下:

<?php

class Clients extends MY_Model {
const DB_TABLE = 'client';
const DB_TABLE_PK = 'client_id';

/**
 * Client's unique identifier.
 * @var int
 */
public $client_id;

/**
 * Client or representative's first name.
 * @var varchar(45)
 */
public $first_name;

/**
 * Client or representitive's last name.
 * @var varchar(45)
 */
public $last_name;

/**
 * Client's address.
 * @var varchar(45)
 */
public $address;

/**
 * Client's apartment or suite number.
 * @var int
 */
public $apt_or_suite_number;

/**
 * Client's postal zip code.
 * @var int
 */
public $zip_code;

/**
 * Client's city.
 * @var varchar(45)
 */
public $city;

/**
 * Two letter state abbreviation of client's state.
 * @var varchar(2)
 */
public $state;

/**
 * Client's email address.
 * @var varchar(45)
 */
public $email;
}

到目前为止我的注册控制器是:

if (!defined('BASEPATH'))
exit('No direct script access allowed');

include_once("analyticstracking.php");

class Registration extends CI_Controller {

public function index() {
    /**
     * Load Models
     */
    $data = array();

    $this->load->model('Clients');
    $this->load->model('Phones');
    $client = new Clients();
    $data['first_name'] = $client;
    $this->load->view('client');

    /**
     * Validation
     */
    $this->load->library('form_validation');
    $this->form_validation->set_rules(array(
        array(
            'field' => 'first_name',
            'label' => 'First Name',
            'rules' => 'required',
        ),
        array(
            'field' => 'last_name',
            'label' => 'Last Name',
            'rules' => 'required',
        ),
        array(
            'field' => 'address',
            'label' => 'Address',
            'rules' => 'required',
        ),
        array(
            'field' => 'apt_or_suite_number',
            'label' => 'Apartment or Suite Number',
            'rules' => '',
        ),
        array(
            'field' => 'zip_code',
            'label' => 'Postal Zip Code',
            'rules' => 'required|is_numeric',
        ),
        array(
            'field' => 'city',
            'label' => 'City',
            'rule' => 'required',
        ),
        array(
            'field' => 'state',
            'label' => 'State',
            'rules' => 'required',
        ),
        array(
            'field' => 'email',
            'label' => 'Email Address',
            'rules' => 'required',
        ),
        array(
            'field' => 'area_code',
            'label' => 'Area Code',
            'rules' => 'required|is_numeric',
        ),
        array(
            'field' => 'number',
            'label' => 'Phone Number (without area code)',
            'rules' => 'required|is_numeric',
        ),
        array(
            'field' => 'extension',
            'label' => 'Extension (if applicable)',
            'rules' => '',
        ),
        array(
            'field' => 'type',
            'label' => 'Type',
            'rules' => '',
        ),
    ));
    $this->form_validation->set_error_delimiters('<div class="alert alert-error"><span style="color: red;">', '</span></div>');
    if (!$this->form_validation->run()) {
        /**
         * Display Forms
         */
        $this->load->view('form_start');
        $this->load->view('client_form');
        $this->load->view('phone_form');
        $this->load->view('submit');
    } else {
        /**
         * Store data and Display Success
         */
        $this->load->model('Clients');
        $client = new Clients();
        $client->client_id = $this->input->post('client_id');
        $client->first_name = $this->input->post('first_name');
        $client->last_name = $this->input->post('last_name');
        $client->address = $this->input->post('address');
        $client->apt_or_suite_number = $this->input->post('apt_or_suite_number');
        $client->city = $this->input->post('city');
        $client->state = $this->input->post('state');
        $client->zip_code = $this->input->post('zip_code');
        $client->save();
        $this->load->view('form_success', array(
            'client' => $client,
        ));
    }
}

}

我的观点是与他们相关的表格。如果你需要它们我可以添加它们。

我一直在关注Lynda.com上的教程来设计我自己的网站,但主持人的动作太快,我无法完全理解发生了什么。我以为我已经使插入语句正确,但显然没有。它所做的只是将数据保存在本地会话中,而不是我能说的最好。

已更新

    <hr>
<legend>Basic Client Information</legend>
<div>
    <label for="first_name">First Name</label>
    <?php echo form_error('first_name'); ?>
    <input type="text" name="first_name" value="<?php echo set_value('first_name'); ?>" placeholder="John" />
</div>

<div>
    <label for="last_name">Last Name</label>
    <?php echo form_error('last_name'); ?>
    <input type="text" name="last_name" value="<?php echo set_value('last_name'); ?>" placeholder="Doe" />
</div>

<div>
    <label for="address">Address</label>
    <?php echo form_error('address'); ?>
    <input type="text" name="address" value="<?php echo set_value('addres'); ?>" placeholder="555 Dream Street"/>
</div>

<div>
    <label for="apt_or_suite_number">Apartment or Suite Number</label>
    <?php echo form_error('apt_or_suite_number'); ?>
    <input type="text" name="apt_or_suite_number" value="<?php echo set_value('apt_or_suite_number'); ?>" placeholder="555"/>
</div>

<div>
    <label for="zip_code">Postal Zip Code</label>
    <?php echo form_error('zip_code'); ?>
    <input type="text" name="zip_code" value="<?php echo set_value('zip_code'); ?>" placeholder="85022"/>
</div>

<div>
    <label for="city">City</label>
    <?php echo form_error('city'); ?>
    <input type="text" name="city" value="<?php echo set_value('city'); ?>" placeholder="Loony Town"/>
</div>

<div>
    <label for="state">State Abbreviation</label>
    <?php echo form_error('state'); ?>
    <input type="text" name="state" value="<?php echo set_value('state'); ?>" placeholder="ID"/>
</div>

<div>
    <label for="email">Email Address</label>
    <?php echo form_error('email'); ?>
    <input type="email" name="email" value="<?php echo set_value('email'); ?>" placeholder="myemail@fake.com"/>
</div>

注意:form_start中包含带有要发布的操作的表单标记。

1 个答案:

答案 0 :(得分:1)

您是否检查过数据是否已插入数据库?应该有。

“严重性”错误通知实际上不是错误,它不是警告。这些“通知”是代码中错误的良好指标。摆脱它们:

  • “未定义的变量:客户端”。您在一行的右侧使用了$ client,例如$ x = $ Client或$ client ++;该变量未定义,因此您要么没有检查它是否正确为空,例如使用isset()或其错字
  • “试图获取非对象的属性”。您已访问过对象,例如$ client-&gt; id,其中$ client不是对象。可能它只是空的,这是你之前的信息所表明的。