Php 如何创建将记录在数据库中并自动显示在表中的数据?
我试图在php中的代码上应用CRUD函数。我刚刚开始使用CodeIgniter来遵循MVC模式 这是我到目前为止所做的,数据库中tbl_数据的记录以HTML格式显示在表中。 请在登录时忽略我的代码。我已经完成了那个代码。谢谢 Model User.php 查看数据\u View.php Controller Data.phpPhp 如何创建将记录在数据库中并自动显示在表中的数据?,php,sql,codeigniter,Php,Sql,Codeigniter,我试图在php中的代码上应用CRUD函数。我刚刚开始使用CodeIgniter来遵循MVC模式 这是我到目前为止所做的,数据库中tbl_数据的记录以HTML格式显示在表中。 请在登录时忽略我的代码。我已经完成了那个代码。谢谢 Model User.php 查看数据\u View.php Controller Data.php 如何创建将记录在数据库中并自动显示在表中的数据?删除功能正在运行。我不知道为什么在我的create函数中,数据没有记录在我的数据库中 您正在调用模型函数,而没有传递正在创建
如何创建将记录在数据库中并自动显示在表中的数据?删除功能正在运行。我不知道为什么在我的create函数中,数据没有记录在我的数据库中 您正在调用模型函数,而没有传递正在创建的数据数组 试着用这种方法
$data = array('title' => $this->input->post('title'), 'author' => $this->input->post('author'), 'content' => $this->input->post('content'));
$this->user->add_record($data);
将您的$data发送到您的add_记录,因为这样,您的add_记录可以访问您输入的值震惊地看到,我仅仅因为解释多一点就投了反对票。做得好。。向你们竖起大拇指。
<!DOCTYPE html>
<html>
<head>
<title>CodeIgniter</title>
<style type="text/css">
label {
display: block;
}
div {
float: left;
padding: 1em;
}
#div2 {
width: 60%;
text-align: center;
}
</style>
</head>
<body>
<div>
<h4>Create Record</h4>
<?php echo form_open('data/create'); ?>
<p>
<label for="title">Title:</label>
<input type="text" id="title" name="title"/>
</p>
<p>
<label for="author">Author:</label>
<input type="text" id="author" name="author"/>
</p>
<p>
<label for="content">Content:</label>
<input type="text" id="content" name="content"/>
</p>
<p>
<input type="submit" value="Submit"/>
<?php echo form_close(); ?>
</p>
</div>
<div id="div2">
<h4>Read Record</h4>
<?php if(isset($records)) : ?>
<table border="1" size="90%">
<tr><th>Title</th><th>Author</th><th>Content</th><th>Admin Tool</th></tr>
<?php foreach($records as $row) : ?>
<tr>
<td><?php echo $row->title; ?></td>
<td><?php echo $row->author; ?></td>
<td><?php echo $row->content; ?></td>
<td>
<?php
echo anchor("data/delete/$row->dataID", 'delete');
echo '/' . anchor("data/update/$row->dataID", 'edit');
?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</div>
<br/>
<h4>Delete Record</h4>
<p>
To delete sample method, simply click on one of the headings listed above.
A delete query will automatically run.
</p>
</body>
</html>
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Data extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('user');
}
function index() {
if ($this->session->userdata('logged_in')) {
$data = array();
if ($query = $this->user->get_records()) {
$data['records'] = $query;
}
$this->load->view('data_view', $data);
} else {
redirect('login','refresh');
}
}
function create() {
$data = array('title' => $this->input->post('title'), 'author' => $this->input->post('author'), 'content' => $this->input->post('content'));
$this->user->add_record();
redirect('data', 'refresh');
}
function update() {
$data = array('title' => 'No title', 'author' => 'me', 'content' => 'this is just a sample');
$this->user->update_record();
}
function delete() {
$this->user->delete_record();
}
}
?>
$data = array('title' => $this->input->post('title'), 'author' => $this->input->post('author'), 'content' => $this->input->post('content'));
$this->user->add_record($data);
$this->user->add_record($data);