Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在MVC中使用oop-php在数据库中插入数据_Php_Codeigniter - Fatal编程技术网

在MVC中使用oop-php在数据库中插入数据

在MVC中使用oop-php在数据库中插入数据,php,codeigniter,Php,Codeigniter,关于insert/index.php 第页的表格 <form action="insert/run" method="post"> <input type="text" name="data"/> <input type="submit" value="Submit"/> </form> 在controller/insert.php上 此页面中的表单呈现 <?php class Insert extends Controller {

关于insert/index.php 第页的表格

<form action="insert/run" method="post">
<input type="text" name="data"/>
<input type="submit" value="Submit"/>
</form>

在controller/insert.php上 此页面中的表单呈现

<?php
class Insert extends Controller 
{
function __construct() {
    parent::__construct();
}
function index() {
    $this->view->render('insert/index');
}
    function run()
    {
            $this->model->run();
    }
}
控制器上的

 function run() {
        $data  = array();

        $data['data'] = $_POST['data'];

        $this->model->run($data);
    }
在你的模型上

public function run($data) {

    $sth = $this->db->prepare("INSERT INTO data (data) values :data");

    $sth->bindValue(":data", $data['data']);

    $sth->execute();
}
控制器:

//add this to your controller
function run()
    {
            $data = array('column1' => $this->input->post('data')
                          );
            $this->model->run($data);
    }
//论模式

    part of code in model

    function run() {
    $data  = array();

    $data['data'] = $_POST['data'];

    $this->model->run($data);
}
//在控制器上

      part of code in controller

      public function run($data)
       {
          $stam =  $this->db->prepare('INSERT INTO data (`data`) VALUES (:data)');

          $stam->execute(array(
           ':data' => $data['data']
                ));
    }

那将是perfact

什么不起作用?它没有上传到你的数据库中吗?你确定你的表名/列名正确吗?你从哪里得到post数据?你想问什么?你想解释一下它是如何发生的。我解决了这个问题,我们在数组中得到数据,这就是为什么我们必须使用数组来执行它。
//change this
 public function run()
    {

        $sth = $this->db->prepare("INSERT INTO data (data) value ");
    }

//to this

public function run($data)
    {

       $this->db->insert("tablename",$data);
    }
//add this to your controller
function run()
    {
            $data = array('column1' => $this->input->post('data')
                          );
            $this->model->run($data);
    }
    part of code in model

    function run() {
    $data  = array();

    $data['data'] = $_POST['data'];

    $this->model->run($data);
}
      part of code in controller

      public function run($data)
       {
          $stam =  $this->db->prepare('INSERT INTO data (`data`) VALUES (:data)');

          $stam->execute(array(
           ':data' => $data['data']
                ));
    }