вставить данные в базу данных в рамках - PullRequest
0 голосов
/ 08 мая 2018

Я новичок в codeigniter. Мне нужно вставить данные в базу данных, для этого я создаю контроллер с именем Home.php, ниже приведен этот код; во время работы я получаю сообщение об ошибке типа Message: Undefined property: 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";
         }
         }
          }

Здесь вид Pahe на имя - 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 голосов
/ 08 мая 2018

Прежде всего вам нужно загрузить модель в ваш контроллер.

 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;
    }
0 голосов
/ 08 мая 2018

Надеюсь, это поможет вам

При загрузке контроллера 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(), тогда URL-адрес формы в порядке, но если нет, то действие формы следует перенаправить на метод 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();
    }
}
...