You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter中创建按钮保存用户输入数据至数据库的技术问询

Hey there! Let's walk through how to tackle both of your requirements using CodeIgniter. I'll break each task into clear, actionable steps covering the View, Controller, and Model layers since that's how CodeIgniter's MVC structure works best.

1. 创建按钮实现用户点击时保存输入数据到数据库

Let's start with the basic form setup to capture user input and save it.

Step 1: Create the View (e.g., views/save_form.php)

First, we'll build a simple form with an input field and a submit button. We'll use CodeIgniter's form_open() helper to handle form submission properly:

<?php echo form_open('save_controller/save_data'); ?>
    <label for="user_input">输入内容:</label>
    <input type="text" name="user_input" id="user_input" required>
    <button type="submit">保存数据</button>
<?php echo form_close(); ?>

Note: Make sure you load the form helper either in your controller or globally in config/autoload.php.

Step 2: Create the Controller (e.g., controllers/Save_controller.php)

Next, we'll create a controller to handle the form submission, validate the input, and call the model to save data:

<?php
class Save_controller extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('Save_model'); // Load the model
        $this->load->helper('form'); // Load form helper if not autoloaded
        $this->load->library('form_validation'); // Load form validation library
    }

    public function save_data() {
        // Validate the input field
        $this->form_validation->set_rules('user_input', '输入内容', 'required');

        if ($this->form_validation->run() == FALSE) {
            // If validation fails, load the form view again
            $this->load->view('save_form');
        } else {
            // Get the input data
            $data = array(
                'input_content' => $this->input->post('user_input')
                // Add other fields if needed, matching your database table columns
            );

            // Call the model to save data
            $this->Save_model->save_to_db($data);

            // Optional: Redirect to a success page or show a message
            echo "数据保存成功!";
            // Or redirect: redirect('save_controller/success');
        }
    }
}
?>

Step 3: Create the Model (e.g., models/Save_model.php)

Now, the model will handle the actual database insertion. Make sure your database is configured correctly in config/database.php:

<?php
class Save_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->load->database(); // Load database connection
    }

    public function save_to_db($data) {
        // Replace 'your_table_name' with your actual database table name
        $this->db->insert('your_table_name', $data);
        return $this->db->insert_id(); // Optional: Return the ID of the inserted row
    }
}
?>
2. 为搜索框创建「Request」按钮并保存输入内容到数据库

This is very similar to the first task—we just adjust the form to use a search input and a "Request" button. The core logic (Controller and Model) can even be reused with minor tweaks.

Step 1: Create the Search View (e.g., views/search_form.php)

Build the search form with a search input and the Request button:

<?php echo form_open('search_controller/save_search_query'); ?>
    <label for="search_query">搜索内容:</label>
    <input type="text" name="search_query" id="search_query" placeholder="输入搜索关键词..." required>
    <button type="submit">Request</button>
<?php echo form_close(); ?>

Step 2: Create the Search Controller (e.g., controllers/Search_controller.php)

This controller will handle the search form submission, validate the input, and save it to the database:

<?php
class Search_controller extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('Search_model'); // Or reuse the earlier Save_model if applicable
        $this->load->helper('form');
        $this->load->library('form_validation');
    }

    public function save_search_query() {
        // Validate the search input
        $this->form_validation->set_rules('search_query', '搜索内容', 'required');

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('search_form');
        } else {
            $data = array(
                'search_content' => $this->input->post('search_query'),
                'created_at' => date('Y-m-d H:i:s') // Optional: Add timestamp for when the search was made
            );

            // Call the model to save the search query
            $this->Search_model->save_search($data);

            // Optional: Redirect to search results page or show success message
            echo "搜索请求已保存!";
            // Or redirect to results: redirect('search_controller/show_results');
        }
    }
}
?>

Step 3: Create the Search Model (e.g., models/Search_model.php)

If you want a separate model for search queries, here's how to set it up. Alternatively, you can extend or reuse the earlier model if your table structure allows:

<?php
class Search_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function save_search($data) {
        // Replace 'search_queries' with your actual table name for storing searches
        $this->db->insert('search_queries', $data);
        return $this->db->insert_id();
    }
}
?>

Quick Notes:

  • Don't forget to configure your database settings in application/config/database.php to connect to your database.
  • Always validate user input to prevent empty submissions and potential security issues (CodeIgniter's form validation library helps with this).
  • You can add flash messages instead of plain echo statements for better user experience—check out CodeIgniter's session library for that.

内容的提问来源于stack exchange,提问作者rashmi ag

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:18:16