【问题标题】:insert data into database in framework在框架中将数据插入数据库
【发布时间】:2018-05-08 05:38:00
【问题描述】:

我是 Codeigniter 的新手。我需要在数据库中插入数据,因为我在名称 Home.php 上创建了一个控制器,下面是该代码;当我运行时,我收到类似消息的错误:未定义的属性:主页::$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);
    }
}

【问题讨论】:

  • Hi @Prem In Home Controller 编辑这样的模型 $this->load->model('Yes', 'yes');然后别名 yes 在 savedata() 函数中使用。谢谢
  • 只是为了让你知道 CI_Models 应该是 CI_Model codeigniter.com/user_guide/general/models.html#what-is-a-model
  • 请始终通过提供一些 cmets 来回应答案,或者,如果对您有帮助,请将其标记为绿色并点赞,这是感谢所有程序员的最佳方式

标签: php codeigniter sql-insert


【解决方案1】:

首先你必须在你的控制器中加载模型。

 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;
    }

【讨论】:

    【解决方案2】:

    希望对您有所帮助

    在控制器中加载databasemodel 中的__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-&gt;load-&gt;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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多