Search code examples
phpcodeignitercodeigniter-3

Simple form to database with Code Igniter 3 not working


I'm trying to make a basic form that the user can fill, so when it submit the form is inserted into a database.

In the views :

<?php echo form_open_multipart(); 
echo form_input(array('id' => 'timeDate', 'name' => 'timeDate'),'','required');
echo form_input(array('id' => 'timeStart', 'name' => 'timeStart'),'','required');
echo form_input(array('id' => 'timeEnd', 'name' => 'timeEnd'),'','required');
echo form_submit('submit' ,'Submit'); 
form_close(); ?>

In the controllers :

if(!empty($_POST['submit'])) {
    $this->load->model('scheduler');
    $timeDate = $_POST['timeDate'];
    $timeStart = $_POST['timeStart'];
    $timeEnd = $_POST['timeEnd'];
    $this->scheduler->add($poll, $timeDate, $timeStart, $timeEnd);
}

In the models :

public function Add($idPoll, $timeDate, $timeStart, $timeEnd) {
    if($this->checkUnique($idPoll, $timeDate, $timeStart, $timeEnd)) {
        
        $this->db->set('idPoll', $idPoll)
        ->set('timeDate', $timeDate)
        ->set('timeStart', $timeStart)
        ->set('timeEnd',  $timeEnd)
        ->insert($this->table);
    }
}

The table:

CREATE TABLE `scheduler` (
    `idScheduler` int(11) UNSIGNED NOT NULL,
    `idPoll` varchar(64) NOT NULL,
    `timeDate` int(15) UNSIGNED NOT NULL DEFAULT 1111,
    `timeStart` int(15) UNSIGNED NOT NULL DEFAULT 1111,
    `timeEnd` int(15) UNSIGNED NOT NULL DEFAULT 1111
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I see nothing wrong with it, but it just does not work (I can insert in the console, but doing it automatically with a form is not working)

Any reasons why ? Thanks !


Solution

  • Could it be that you are not passing a 'idScheduler' and that cannot be NULL?

    Have you defined "$this->table" somewhere? Try using the table name.