【问题标题】:Not able to insert into database using CodeIgniter无法使用 CodeIgniter 插入数据库
【发布时间】:2014-05-01 11:57:27
【问题描述】:

我正在尝试通过 Codeigniter 将数据插入数据库,但无法继续。我有一个数据库mydatabase 有5 个表training_assignment_idassignment_nameassignment_descriptionrecordings_requiredis_activetraining_assignment_id 是自动递增的。

Controller-1 training.php

<?php 

class Training extends MY_Controller {


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

 }

 function index()
 {

   $this->load->helper(array('form'));
   $this->load->view('training_admin');

 }
 /*
 function input()
 {
     $this->load->model('training');
     if($this->input->post('mysubmit')){
        $this->training->training_assignment_insert();
    } 
 }*/
}

?>

Controller-2 add_training.php

<?php 

class Add_training extends CI_Controller {

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

 /*function index($assignment, $description, $check, $radio)
 {
   $assignment=$this->input->post('assignment');
   $description=$this->input->post('assignment2');
   $check=$this->input->post('assignment3');
   $radio=$this->input->post('is_active');

   //query the database


   if($assignment!=NULL || $description!=NULL || $check!=NULL || $radio!=NULL)
   {
     $this->training->training_assignment_insert($assignment, $description, $check, $radio);
     echo 'inserted successfully';
   }
 else {
     echo "Data has not been inserted";      
   }  

 }*/

 function index()
 {

     $this->training->training_assignment_insert(); // this should forward them to the Model

    }
 }

?>

模型training.php

<?php
Class Training extends CI_Model
{
 function __construct()
        {
            parent::__construct();
            $this->load->database();
        }
 function training_assignment_insert($data ){

$data = array(
        'assignment' => $this->input->post('assignment', TRUE),
        'description' =>$this->input->post('assignment2', TRUE),
        'check' => $this->input->post('assignment3', TRUE),
        'radio' => $this->input->post('is_active', TRUE)
     );
    $this->db->insert('training_assignment', $data);
 }
}
?>

查看training_admin.php

    <html>
 <head>

<link rel="stylesheet" type="text/css" href="<?php echo base_url() ?>resources/training/mystyle.css">
    <title>Assignment</title>
 </head>
 <body>
  <div id="container">
   <h1>Add Assignment</h1>
   <div id="body">
   <?php echo form_open('traning/add_training'); ?>
   <div id="label">

            <input type="text" name="assignment" placeholder="Assignment Name" style="margin-left: 15px;"/>
            <br/>
            <textarea rows="4" cols="30" name="assignment2" placeholder="Assignment Description" style="margin-left: 15px;"></textarea>
            <br/>
            <label>Recording Require: </label>
            <select name="assignment3">
            <option value="1">Yes</option>
            <option value="0">No</option>
            </select>
            <br/>
            <label for="active">Enable </label>
            <input type="radio" name="is_active" id="enable" value="1"/>
            <label for="female">Disable </label>
            <input type="radio" name="is_active" id="disable" value="0"/>
            <br/>
        <input class="class1" type="submit" value="Assign"/>
   </div>
     <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
     </div>
     </div>
   </form>
 </body>
</html>

请任何人帮助我,我是 CodeIgniter 的新手。

【问题讨论】:

  • 我看不到具有 add_training 功能的训练控制器
  • 您需要在模型函数模型函数中传递您的帖子值,不要直接获取帖子值
  • Mohit add_training 也是一个控制器。这里 form_open('traning/add_training'); traning 是模块的主文件夹。
  • Mohit 不是 2 控制器 form_open('traning/add_training');我在这里给出了路径。 add_training 只是控制器“培训”是我模块的文件夹名称。不要混淆
  • 你为我们提供了 training.php 我只是认为它是你的控制器

标签: php mysql codeigniter


【解决方案1】:

这样做

控制器

  <?php 

class Add_training extends CI_Controller {

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

 function index()
 {
 $insert = array(
        'assignment' => $this->input->post('assignment', TRUE),
        'description' =>$this->input->post('assignment2', TRUE),
        'check' => $this->input->post('assignment3', TRUE),
        'radio' => $this->input->post('is_active', TRUE)
     );
     $this->training->training_assignment_insert($insert); // this should forward them to the Model

    }
 }

?>

型号

 <?php
    Class Training extends CI_Model
    {
     function __construct()
            {
                parent::__construct();
            //    $this->load->database();
            }
     function training_assignment_insert($data ){
              $query = $this->db->insert('training_assignment', $data);
             if($query){
                  return true;
               }
           }
    }
    ?>

【讨论】:

    【解决方案2】:

    您需要将帖子值传递给模型尝试 控制器

    function index()
     {
         $data = array(
            'assignment' => $this->input->post('assignment', TRUE),
            'description' =>$this->input->post('assignment2', TRUE),
            'check' => $this->input->post('assignment3', TRUE),
            'radio' => $this->input->post('is_active', TRUE)
         );
         $this->training->training_assignment_insert($data); // this should forward them to the Model
    
        }
    

    和型号:-

    function training_assignment_insert($data ){
    
        $this->db->insert('training_assignment', $data);
     }
    

    【讨论】:

    • 无变化 Rakesh :-/
    猜你喜欢
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多