Php 发出以将数据发送到数据库
我写了下面的代码,但它给了我几个错误(在代码后面列出)。我不知道怎么解决它 模型->新闻\u模型Php 发出以将数据发送到数据库,php,database,codeigniter,Php,Database,Codeigniter,我写了下面的代码,但它给了我几个错误(在代码后面列出)。我不知道怎么解决它 模型->新闻\u模型 <?php class News_model extends CI_Model { public function __construct() { $this->load->database(); } public functi
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
}
?> ```
**Controller -> News.php **
```<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('News_model');
$this->load->helper('url_helper');
}
public function index()
{
//$this->load->model('News_model');
$data['news'] = $this->News_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug = NULL)
{
$data['news_item'] = $this->News_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'Text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
}
```
**Controller->News.php**
```
标题
正文
view->views/news/index.php
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
<label for="title">Title</label>
<input type="text" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
<h2><?php echo $title; ?></h2>
<?php foreach ($news as $news_item): ?>
<h3><?php echo $news_item['title']; ?></h3>
<div class="main">
<?php echo $news_item['text']; ?>
</div>
<p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a></p>
<?php endforeach; ?>
<!DOCTYPE html>
<html>
<head>
<title>Success</title>
</head>
<body>
<h2>Succeed</h2>
</body>
</html>
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
views->views/news/success.php
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
<label for="title">Title</label>
<input type="text" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
<h2><?php echo $title; ?></h2>
<?php foreach ($news as $news_item): ?>
<h3><?php echo $news_item['title']; ?></h3>
<div class="main">
<?php echo $news_item['text']; ?>
</div>
<p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a></p>
<?php endforeach; ?>
<!DOCTYPE html>
<html>
<head>
<title>Success</title>
</head>
<body>
<h2>Succeed</h2>
</body>
</html>
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
成功
成功
views->views/news/view.php
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
<label for="title">Title</label>
<input type="text" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
<h2><?php echo $title; ?></h2>
<?php foreach ($news as $news_item): ?>
<h3><?php echo $news_item['title']; ?></h3>
<div class="main">
<?php echo $news_item['text']; ?>
</div>
<p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a></p>
<?php endforeach; ?>
<!DOCTYPE html>
<html>
<head>
<title>Success</title>
</head>
<body>
<h2>Succeed</h2>
</body>
</html>
<?php
echo '<h2>'.$news_item['title'].'</h2>';
echo $news_item['text'];
在此处将新闻模型更改为新闻模型
请在控制器中加载您的模型,其中使用
$this->load->model('News_model');
和使用
$this->News_model->set_news();
在create()文件名:C:\xampp\htdocs\oproject\application\controllers\News.php行号:72 Backtrace:File:C:\xampp\htdocs\oproject\index.php行号:315 Function:require\u once`所以在News\u model文件中没有函数集\u News(),那么为什么在这里调用该函数呢?我想有一个:public Function set\u News()方法在News\u模型中,,,您能检查一下吗?模型中没有set\u News()函数,您将该函数放置在控制器中,请检查它