【问题标题】:How to insert data into database in codeigniter? [closed]如何在codeigniter中将数据插入数据库? [关闭]
【发布时间】:2016-03-13 10:01:37
【问题描述】:

我是这项工作的新手,我在视图文件夹中创建了简单的表单,如下所示:

 <form action="" method="post" enctype="multipart/form-data">
       <table>
                <tr>
                	<td>Book Title</td>
                    <td><input type="text" name="title" /></td>
                </tr>
                <tr>
                	<td>Book Author</td>
                    <td><input type="text" name="author" /></td>
                </tr>
                <tr>
                	<td>Book Image</td>
                    <td><input type="file" name="image" /></td>
                </tr>
               
                <tr>
                	<td>Book Description</td>
                    <td><input type="text" name="content" /></td>
                </tr>
                <tr>
                    <td><input type="submit" name="submit" /></td>
                </tr>
        </table>
        </form>

我在控制器中接收表单数据,如下所示:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

	
	public function index()
    	{
    		$this->load->view('insert/post');
            
           $title = $this->input->post('title');
           $author = $this->input->post('author');
           $image = $this->input->post('image');
           $content = $this->input->post('content');
                               
    	}
    
  }

但是如何将这些数据插入数据库,请使用简单的代码。

【问题讨论】:

  • 请不要仅仅要求某人为您编码而提出问题。谷歌上有很多很多教程可以让你写得简单。那么,如果您编写的代码不起作用,请来这里寻求帮助。

标签: php codeigniter model-view-controller


【解决方案1】:

给你

应用程序/控制器/Welcome.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {


    public function index()
    {   
        // If you have post data...
        if (!empty($_POST)) {
            $title = $this->input->post('title');
            $author = $this->input->post('author');
            $image = $this->input->post('image');
            $content = $this->input->post('content');
            // Checking if everything is there
            if ($title && $author && $image && $content) {
                // Loading model
                $this->load->model('exemple_model');
                $data = array(
                    'title' => $title,
                    'author' => $author,
                    'image' => $image,
                    'content' => $content
                );

                // Calling model
                $id = $this->exemple_model->insert($data);

                // You can do something else here
            }
        }
        // Loading view
        $this->load->view('insert/post');                       
    }

}

应用程序/模型/Exemple_model.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Exemple_model extends CI_Model {


    public function insert($data) {
        // Inserting into your table
        $this->db->insert('MyTable', $data);
        // Return the id of inserted row
        return $idOfInsertedData = $this->db->insert_id();
    }

}

【讨论】:

  • 我确实喜欢这个,但数据仍然没有插入到数据库中,
  • 嗯,你一定是做错了什么,当你的 sql 查询出错时 CodeIgniter 会显示错误。你的完整代码是什么?你永远不会在你的 sn-p 中调用模型
  • 我试过和你定义的一样: db->insert('bookdata', $data);返回 $idOfInsertedData = $this->db->insert_id(); } }
  • 你在用我给你的吗?如果是这样,请确保每个 vars 都在控制器中填充了一个值,必要时 var_dump 它们
  • 是的,我正在使用你给我的,每个变量都在控制器中填充了一个从表单获取的值。
【解决方案2】:
$this->db->query(
    'INSERT INTO tbl (a, b, c, d) VALUES (?, ?, ?, ?)',
    array($a, $b, $c, $d)
);

$this->db->insert('tbl', array($a, $b, $c, $d));

获取最后插入 ID:

$idOfInsertedData = $this->db->insert_id();

【讨论】:

  • 但是如何将这些表单值发送到模型?我将在哪里运行这个插入查询。
  • @ZubairAli 只需将此代码放入您的模型中
猜你喜欢
  • 2016-04-06
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 1970-01-01
  • 2014-05-04
  • 1970-01-01
  • 2016-10-21
  • 2015-08-04
相关资源
最近更新 更多