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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?