dongmi1941 2014-11-25 00:10
浏览 76
已采纳

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

I am trying to connnect to my database on my local machine using code igniter. I views running, but am recieving the following error:

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

The second error I am recieving is as follows:

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

I went to check my database in mySQL Workbench and there is no data entered into database. My CORE model's code is as follows:

<?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;
}
}

My client model is as follows:

<?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;
}

My registration controller thus far is:

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,
        ));
    }
}

}

My views are the forms associated with them. If you need them I can add them.

I have been following a tutorial on Lynda.com to get my own site designed, but the presenter moves far too fast for me to understand completely what is going on. I thought I had gotten the insert statements correct, but apparently not. All it does is keep the data in the local session from what I can best tell.

UPDATED

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

NOTE: That the form_start has the form tag in it with the action to post.

  • 写回答

1条回答 默认 最新

  • doukan5332 2014-11-25 03:52
    关注

    Have you checked if data has been inserted into the database? It should have.

    An error "of severity" Notice is not really an error, its less than a warning. These "Notices" are good indicators of errors in your code. To get rid of them:

    • "Undefined variable: client". You have used $client on the right hand side of a line, such as $x = $Client or $client++; The variable is not defined, so you either have not checked the case of it being empty properly, for example using isset() or its a typo
    • "Trying to get property of non-object". You have accessed an object, for example $client->id where $client is not an object. Probably it is simply empty, which is what your message before indicates.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料