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

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

到目前为止我的注册 Controller 是:

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 上的教程来设计自己的网站,但演示者移动得太快,我无法完全理解正在发生的事情。我以为我的插入语句是正确的,但显然不是。它所做的只是将数据保存在本地 session 中,据我所知。

已更新

    <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 中包含表单标签以及要发布的操作。

最佳答案

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

“严重”错误通知并不是真正的错误,它比警告要少。这些“通知”可以很好地指示代码中的错误。要摆脱它们:

  • “ undefined variable :客户端”。您已在该行的右侧使用了 $client,例如 $x = $Client 或 $client++;该变量未定义,因此您没有正确检查它是否为空,例如使用 isset() 或其拼写错误
  • “尝试获取非对象的属性”。您访问了一个对象,例如 $client->id,其中 $client 不是对象。也许它只是空的,这就是您之前的消息所表明的。

关于php - 如何使用 Code Igniter 框架将数据插入表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27116568/

相关文章:

javascript - 如何在 LiveCycle 中对表行进行部分求和 - Javascript

php - Linux 下的 Codeigniter 文件名和加载助手

php - 通过 http 请求从名称服务器获得响应?

PHP代码判断两条线是否平行

php - 在Symfony(PHP)中验证 Action

php - PHP 函数 in_array(...) 有什么问题?

mysql - 一组数据结构的变化类似于SVN?

mysql - 如何获取特定列未链接到另一个表的特定值的数据库表条目?

php - $this->uri->segment(3) 在 codeigniter 分页中的用途是什么

php - 使用 codeigniter php 加载多个 View