在框架中将数据插入数据库

时间:2018-05-08 05:38:00

标签: php codeigniter sql-insert

我是codeigniter的新人。我需要在数据库中插入数据,因为我在名称Home.php上创建一个控制器,下面是该代码;当我正在运行时,我收到错误消息:未定义属性:Home :: $ Yes.Kindly help拜托,先谢谢

 <?php
   defined('BASEPATH') OR exit('No direct script access allowed');
   class Home extends CI_Controller{
        public function index()
    {
        $this->load->database();
        $this->load->model('yes');

    }
    public function savedata()
    {
        $this->load->view('demo');
        if($this->input->post('submit'))
        {
        $name=$this->input->post('name');
        $email=$this->input->post('email');
        $content=$this->input->post('content');
        $this->Yes->saverecords($name,$email,$content);     
        echo "Records Saved Successfully";
         }
         }
          }

这里是名称的视图 - demo.php

<html>
<head>
    <title></title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data"  
 id="User">     


               <h2 align="center">Student Marks</h2>
               <h3>Name:</h3>               
               <input type="text" name="name" />        
               <h3>Email ID</h3> 
               <input type="text" name="email" />       
               <h3>Content </h3>  
               <input type="text" name="content" />   


        <button type="submit" value="submit" name="submit" >Submit</button>
        <button type="reset" value="Reset" >Reset</button> 


        </form>

</body>
</html>

这是Yes.php的模型页面

<?php
class Yes extends CI_Models
{
    function saverecords($name,$email,$content)
    {
    $query="insert into news values('$name','$email','$mobile')";
    $this->db->query($query);
    }
}

2 个答案:

答案 0 :(得分:0)

希望这会对您有所帮助

在这样的database中的控制器加载model__construct中,您只是将其添加到index方法

public function __construct()
{
   parent::__construct();
   $this->load->database();
   $this->load->model('yes');
}

public function index()
{
}

public function savedata()
{
    if($this->input->post('submit'))
    {
        $name = $this->input->post('name');
        $email = $this->input->post('email');
        $content = $this->input->post('content');

        $data = ['name' => $name,'email' => $email,'content' => $content];
        $insert_id = $this->yes->saverecords($data); 
        if ($insert_id)
        {
            echo "Records Saved Successfully";
        }    

    }
    $this->load->view('demo');
}

第二

$this->load->view('demo');方法中savedata()是您的表单,然后表单网址即可,但如果没有 表单操作应重定向到savedata方法,因为您正在访问其中的帖子值

<form method="post" action="<?=base_url('home/savedata');?>" enctype="multipart/form-data">

第三,你的模型应该是这样的:

<?php
class Yes extends CI_Models
{
    function saverecords($data)
    {
       $this->db->insert('table_name', $data);
       return $this->db->insert_id();
    }
}

答案 1 :(得分:0)

首先,您必须在控制器中加载模型。

 defined('BASEPATH') OR exit('No direct script access allowed');
   class Home extends CI_Controller{
 function __construct() {
        parent::__construct();
        $this->load->model('Yes');

    }
 public function savedata()
    {
        if($this->input->post('submit'))
        {
       insert_array=array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'content' => $this->input->post('content')
);
$data_id = $this->Yes->saverecords($insert_array);
if($data_id >0){
echo "Records Saved Successfully";
}
         }
         }
}

在模型中

function saverecords($insert_array) {
        if ($this->db->insert('news', $insert_array)) {
            return $this->db->insert_id();
        }
        return 0;
    }