codeigniter中的插入失败

时间:2014-02-05 05:45:40

标签: php mysql codeigniter

我是CodeIgniter的新手,想要将数据插入到我的表中。但我无法这样做。已配置数据库。 任何帮助将不胜感激。

控制器 - show.php

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

class Show extends CI_Controller {

    function index() {
        $this->load->view('you_view');

    }

    public function get() 
            {
        $this->load->model('show_model');
        if ($this->input->post('submit')) 
            {

                $this->show_model->insert();
        }
        redirect('show');
}

}

?>

模型 - show_model.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Show_model extends CI_Model
{
    function __construct() {
        parent::__construct();
    }

    function insert($dbdata)
    {
        $name=  $this->input->post('name');
        $surname=  $this->input->post('surname');
        $data=array(
            'name'=>$name,
            'surname'=>$surname
        );
        $this->db->insert('user', $data);
    }


}
?>

查看 - you_view.php

<?php
        echo form_open('show/get', array('name' => 'myform'));    
    ?>
<form name="form1" method="post" >
    <input type="text" name="name"/> 
    <input type="text" name="surname"/> 
    <button name="submit" id="submit">Submit</button>
</form>
<?php
    echo form_close();
    ?>

database.php -

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'simple';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

7 个答案:

答案 0 :(得分:3)

致电

echo form_open();

您不再需要任何<form>标记了 并在您的模型更改

function insert($dbdata)

function insert($table,$data)
    {
        $this->db->insert($table,$data);

        if($this->db->affected_rows() >0){
            return true;
        } else {
            return false;
        }
    }

在你需要的控制器中

public function get() 
            {
        $this->load->model('show_model');
        if ($this->input->post('name') && $this->input->post('surname')) 
            {
$u = $this->input->post('name');
$sur = $this->input->post('surname');
   $array = array(
'field1'=>$u,
'field2'=>$sur //your feild in your table
);
              $q =   $this->show_model->insert('yourTable',$array);

       if($q === true) redirect('show');
}

愿它有所帮助

答案 1 :(得分:0)

您没有将数据传递给模型插入函数。检查以下行

$this->show_model->insert();

但你在模型功能中有参数。 您应该在控制器本身中进行操作,而不是在模型中准备数据数组。你不应该在模型中使用post。

if ($this->input->post('submit')) 
{
   $name=  $this->input->post('name');
   $surname=  $this->input->post('surname');
   $data=array(
            'name'=>$name,
            'surname'=>$surname
       );
  $this->show_model->insert($data);
}

删除模型中的以下行:

        $name=  $this->input->post('name');
        $surname=  $this->input->post('surname');
        $data=array(
            'name'=>$name,
            'surname'=>$surname
        );

答案 2 :(得分:0)

在您的模型中替换

function insert($dbdata)

function insert()

这是因为在您的控制器中,您将模型函数称为

$this->show_model->insert();

你还没有将任何参数传递给模型函数。

答案 3 :(得分:0)

您应该在Controller中获取输入数据并将其发送到您的模型。 类似的东西:

if ($this->input->post('submit')) 
        {
            $this->show_model->insert(array('name'=>$this->input->post('name'),'surname'=>$this->input->post('surname');));
    }

模特:

$this->db->insert('user', $dbdata);

当然,在插入db之前你还需要trim()和htmlspecialchars(),但这是另一个历史。

答案 4 :(得分:0)

echo form_open(); can be used instead of `<form>` tag.. Don't use `<form>` tag then..

此外,您没有将任何参数从控制器中的函数insert()传递给模型..但是您在模型函数中使用它..请不要这样做..

如果您在localhost中,请访问codeigniter目录中的userguide。它可能类似于localhost / CodeIgniter_2.1.4 / user_guide /

谢谢

答案 5 :(得分:0)

在“查看”页面中,使用form_open调用该函数。

它会将给定数据发送到控制器页面

<?php echo form_open('controller_page/insert'); ?>

在控制器页面中,

`$这 - &GT;负载&GT;模型( 'model_page');

$ name = $ this-&gt; input-&gt; post('name');

$ surname = $ this-&gt; input-&gt; post('surname');

    $this->model_page->insert($name,$surname);`

在模型页面中,

`function insert($ name,$ surname)

{

//提供插入查询

} `

使用上面的代码,它会起作用......

答案 6 :(得分:0)

我希望这会有所帮助。在插入数据之前也要进行表单验证

控制器 - show.php

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

class Show extends CI_Controller {

    function index() {
        $this->load->view('you_view');
    }

    public function get() {
        $this->load->model('show_model');
        if ($this->input->post('submit')) {

            $this->load->library('form_validation');
            $this->form_validation->set_rules('name','Name','trim|required');
            $this->form_validation->set_rules('surname','Surname','trim|required');
            if($this->form_validation->run()==FALSE){
                // DO WHAT YOU WANT TO DO IF NO DATA IS ENTERED
            }else{
                $data=array(
                    'name'=>$this->input->post('name'),
                    'surname'=>$this->input->post('surname')
                );
                $result=$this->Show_model->insert($data);
            }
        }
        redirect('show');
    }

} ?>

模型 - show_model.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Show_model extends CI_Model{

    function __construct() {
        parent::__construct();
    }

    function insert($dbdata){

        $this->db->insert('user', $data);
    }
}
?>

查看 - you_view.php

<?php echo form_open('show/get', array('name' => 'myform'));
      echo form_input('name',,);
      echo form_input('surname',,);
      echo form_submit('submit','Submit'); 
      echo form_close();
?>