Php 如何使用CodeIgniter框架将数据插入表中?

Php 如何使用CodeIgniter框架将数据插入表中?,php,mysql,codeigniter,insert,Php,Mysql,Codeigniter,Insert,我正在尝试使用CodeIgniter连接到本地计算机上的数据库。我查看正在运行,但收到以下错误: 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

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

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
<?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;
}
}
我去mySQL工作台检查了我的数据库,并没有数据输入数据库。我的核心模型代码如下:

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: views/client.php
Line Number: 1
<?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;
}
}

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

“严重性”错误通知并非真正的错误,而不是警告。这些“通知”是代码中错误的良好指示器。要摆脱它们:

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

你能展示你的
视图/client.php
文件吗?我想你是想让我发布view/client\u form.php文件,这就是我刚才添加的。你的问题现在解决了吗?不完全是,我能够从计算表中的行数中得到值,但我希望我能知道如何将该值作为一个整数来获取,这样就可以将它正确地传递给一个编写函数,以便将其写入另一个表的外键字段中。您需要将其放在另一个问题中,因为这完全与当前表有关。
    <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>