• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't insert new record to MySQL Database

#1
[eluser]Unknown[/eluser]
hello guys,
i'm kinda new to CI so can't use it properly even for a simple matter like insert new record..

please don't order me to googling because I already did it first and still stuck

and sorry for bad english..

so this is the MVC of mine

Model
Code:
<?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Test_model extends CI_Model{
        public function insert($data){
            return $this->db->insert_string('testinsert', $data);
        }
    }
?>

Control
Code:
<?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Test_controller extends CI_Controller{
        function __construct() {
            parent::__construct();
            $this->load->model('test_model', '', true);
        }
        
        public function index(){
            $this->load->view('test_view');
        }
        
        public function inserts(){
            $this->form_validation->set_rules('id', 'ID', 'trim|required');
            $this->form_validatoin->set_rules('deskripsi', 'DESKRIPSI', 'trim|required');
            
            $data = array('id'=> $this->input->post('id'), 'deskripsi'=>$this->input->post('deskripsi'));
            
            $st = $this->test_model->insert($data);
            echo $st;
        }
    }
?>

View
Code:
<!DOCTYPE HTML>
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Test Insert&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;?php echo validation_errors(); ?&gt;
        &lt;?php echo form_open('test_controller'); ?&gt;
        <table>
            <tr>
                <td>id</td>
                <td>:</td>
                <td>&lt;input type="text" name="id" /&gt;&lt;/td>
            </tr>
            <tr>
                <td>deskripsi</td>
                <td>:</td>
                <td>&lt;input type="text" name="deskripsi" /&gt;&lt;/td>
            </tr>
            <tr>
                <td colspan="2">&lt;input type="submit" value="Submit" /&gt;&lt;/td>
            </tr>
        </table>
    &lt;/body&gt;
&lt;/html&gt;

#2
[eluser]nabin das[/eluser]
Controller:-
&lt;?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Test_controller extends CI_Controller{
function __construct() {
parent::__construct();
$this->load->model('test_model', '', true);
}

public function index(){
$this->load->view('test_view');
}

public function inserts(){
$this->form_validation->set_rules('id', 'ID', 'trim|required');
$this->form_validatoin->set_rules('deskripsi', 'DESKRIPSI', 'trim|required');
$this->text_model->insert();

}
}
?&gt;

Model:-
class Test_model extends CI_Model{
public function insert(){
$this->load->helper('url');
}
}
$data = array(
'id' => $this->input->post('id'),
'deskripsi' => $this->input->post('deskripsi')
);

return $this->db->insert('table_name', $data);


try with this. hope it will work fine

#3
[eluser]Unknown[/eluser]
I already copy both of your code and
this is the error
Model
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Loader::$input

Filename: models/test_model.php

Line Number: 10

Fatal error: Call to a member function post() on a non-object in /home/pgmr4/public_html/testInsert/application/models/test_model.php on line 10

and I try to solve it by change the code like this

Code:
&lt;?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Test_model extends CI_Model{
        public function insert(){
            $this->load->helper('url');
            
            $data = array('id' => $this->input->post('id'),
                  'deskripsi' => $this->input->post('deskripsi')
            );

            return $this->db->insert('table_name', $data);
        }
    }
?&gt;

and still no data in my database

regards


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.