如何创建将记录在我的数据库中并将自动显示在我的桌面上的数据?

时间:2014-08-05 03:57:19

标签: php sql codeigniter

我正在尝试在我的代码中应用CRUD函数。我刚刚使用CodeIgniter跟踪MVC模式。

这是我到目前为止所做的,我的数据库上的tbl_data记录显示在HTML表格中。 (请在登录时忽略我的代码。我已经完成了那段代码。谢谢。)

模型User.php

<?php 

class User extends CI_Model {

    function login($username, $userpassword) {
        $this->db->select('userid, username, userpassword');
        $this->db->from('tbl_user');
        $this->db->where('username', $username);
        $this->db->where('userpassword', MD5($userpassword));
        $this->db->limit(1);

        $query=$this->db->get();
        if($query->num_rows()==1) {
            return $query->result();
        }
        else {
            return FALSE;
        }
    }

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

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

    function update_record($data) {
        $this->db->where('dataid', 9);
        $this->db->update('tbl_data', $data);
    }

    function delete_record() {
        $this->db->where('dataID', $this->uri->segment(3));
        $this->db->delete('tbl_data');
        redirect('data', 'refresh');
    }
}
?>

查看Data_View.php

<!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>

Controller Data.php

<?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(); 
    }
}
?>

如何创建将记录在我的数据库中并将自动显示在我的桌面上的数据?删除功能正在运行。我不知道为什么在我的创建函数中数据没有记录在我的数据库中。

2 个答案:

答案 0 :(得分:0)

 $this->user->add_record($data);

将您的$数据发送到您的add_record,因此您的add_record可以访问您输入的值

答案 1 :(得分:-2)

您正在调用Model函数而不传递您正在创建的数据数组 尝试使用这种方式。

$data = array('title' => $this->input->post('title'), 'author' => $this->input->post('author'), 'content' => $this->input->post('content'));
$this->user->add_record($data);