1

大家好,我有这段代码,但我不确定我做错了什么,据我所知,数据正在被定义,这些是我得到的错误:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: models/site_model.php

Line Number: 14
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/BLOCK/system/core/Exceptions.php:185)

Filename: core/Common.php

Line Number: 442
A Database Error Occurred

You must use the "set" method to update an entry.

Filename: /Applications/XAMPP/xamppfiles/htdocs/BLOCK/models/site_model.php

Line Number: 14

控制器:

<?php
class Site extends CI_Controller {

function index(){

    $this->load->view('option_view');
}

function create(){

    $data = array(
        'subject' => $this->input->post('subject'),
        'body' => $this->input->post('body')
    );

    $this->Site_model->add_record($data);
    $this->index();

}

}


?>

模型:

<?php

class Site_model extends CI_Model {

function get_records()

{
    $query = $this->db->get('items');
    return $query->result();
}

function add_record()
{
    $this->db->insert('items', $data);
    $return;
}

function update_record()
{
    $this->db->where('id', 1);
    $this->db->update('items', $data);

}

function delete_record()
{
    $this->db->where('id', $this->url->segment(3));
    $this->db->delete('items');

}

}





?>

和观点:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>option_view</title>
<style type="text/css" media="screen">
label {display:block;}
</style>
</head>

<body>
<h2>Create</h2>
<?php echo form_open('site/create'); ?>

<p>
    </label for="subject">Subject</label>
    <input type="text" name="subject" id="subject">
</p>

<p>
    </label for="body">Body</label>
    <input type="text" name="body" id="body">
</p>
<p>
<input type="submit" value="Submit">    
</p>
<?php echo form_close();?>
 </body>
 </html>

大家怎么看?

非常感激

4

3 回答 3

3

你永远不会传递$data给你的方法。这是一个范围问题。

function add_record()
{
    $this->db->insert('items', $data);
    $return;
}

在这种情况下,add_record()不知道是什么$data(并将其视为null尚未定义。

变量范围

于 2012-07-31T20:22:40.487 回答
2

您的模型中的方法不需要参数。例如:

改变:

function add_record()

至:

function add_record($data)
于 2012-07-31T20:22:28.030 回答
0

在这些方法中:

function add_record()
{
    $this->db->insert('items', $data);
    $return;
}

function update_record()
{
    $this->db->where('id', 1);
    $this->db->update('items', $data);

}

您尚未将数据传递给方法,因此 $data 未定义。

于 2012-07-31T20:23:03.070 回答