2016-03-13 2 views
-3

Я новичок в этой работе я создал простую форму в папку вида, как это:Как вставить данные в базу данных в codeigniter?

<form action="" method="post" enctype="multipart/form-data"> 
 
     <table> 
 
       <tr> 
 
       \t <td>Book Title</td> 
 
        <td><input type="text" name="title" /></td> 
 
       </tr> 
 
       <tr> 
 
       \t <td>Book Author</td> 
 
        <td><input type="text" name="author" /></td> 
 
       </tr> 
 
       <tr> 
 
       \t <td>Book Image</td> 
 
        <td><input type="file" name="image" /></td> 
 
       </tr> 
 
       
 
       <tr> 
 
       \t <td>Book Description</td> 
 
        <td><input type="text" name="content" /></td> 
 
       </tr> 
 
       <tr> 
 
        <td><input type="submit" name="submit" /></td> 
 
       </tr> 
 
     </table> 
 
     </form>

и я получать данные формы в контроллере, как это:

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

 
class Welcome extends CI_Controller { 
 

 
\t 
 
\t public function index() 
 
    \t { 
 
    \t \t $this->load->view('insert/post'); 
 
      
 
      $title = $this->input->post('title'); 
 
      $author = $this->input->post('author'); 
 
      $image = $this->input->post('image'); 
 
      $content = $this->input->post('content'); 
 
           
 
    \t } 
 
    
 
    }

но как вставить эти данные в базу данных любезно помогают простым кодам.

+0

пожалуйста, не задавать вопросы только просить кого-то для вас код. в Google есть много, много учебников, которые позволят вам писать простые. то, приди сюда, прося о помощи, если код, который вы написали, не работает. –

ответ

0
$this->db->query(
    'INSERT INTO tbl (a, b, c, d) VALUES (?, ?, ?, ?)', 
    array($a, $b, $c, $d) 
); 

или

$this->db->insert('tbl', array($a, $b, $c, $d)); 

Получить последнюю вставку ID:

$idOfInsertedData = $this->db->insert_id(); 
+0

, но как отправить эти значения формы в модель? где я запустил этот запрос вставки. –

+0

@ ZubairAli просто поместите этот код в свою модель – Deep

2

Здесь вы идете

приложение/контроллеры/Welcome.php

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

class Welcome extends CI_Controller { 


    public function index() 
    { 
     // If you have post data... 
     if (!empty($_POST)) { 
      $title = $this->input->post('title'); 
      $author = $this->input->post('author'); 
      $image = $this->input->post('image'); 
      $content = $this->input->post('content'); 
      // Checking if everything is there 
      if ($title && $author && $image && $content) { 
       // Loading model 
       $this->load->model('exemple_model'); 
       $data = array(
        'title' => $title, 
        'author' => $author, 
        'image' => $image, 
        'content' => $content 
       ); 

       // Calling model 
       $id = $this->exemple_model->insert($data); 

       // You can do something else here 
      } 
     } 
     // Loading view 
     $this->load->view('insert/post');      
    } 

} 

применение/модели/Exemple_model.php

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

class Exemple_model extends CI_Model { 


    public function insert($data) { 
     // Inserting into your table 
     $this->db->insert('MyTable', $data); 
     // Return the id of inserted row 
     return $idOfInsertedData = $this->db->insert_id(); 
    } 

} 
+0

Мне это понравилось, но данные по-прежнему не вставляются в базу данных, –

+0

Ну, вы должны что-то делать неправильно, CodeIgniter отображает ошибку, когда ваш SQL-запрос неверен. Каков ваш полный код? Вы никогда не называйте модель в вашем фрагменте – Gwendal

+0

я попытался так же, как вы определили: db-> вставка ('bookdata ', $ data); return $ idOfInsertedData = $ this-> db-> insert_id(); } } –